2852. 统计字母频率

10175102262 LarsPendragon

C语言(C班)B题题解
fgets()函数可以用来直接读取一行输入。参数:地址、数量、输入流(这里是标准输入stdin)

#include <stdio.h>
int main()
{
    double p[26]={0}, max=0;
    char word[4000];
    int i, mi, cnt=0;
    fgets(word, 4000, stdin);
    char *a=word;
    while(*a!='\n')
    {
        if(*a==' ') {a++; continue;}
        else {p[*a-'a']++; cnt++;}
        a++;
    }
    for(i=0; i<26; i++) if(max<p[i]) {max=p[i]; mi=i;}
    printf("%c %.2lf\n",'a'+mi, max/cnt);
    return 0;
}
zzpzbl

那个a!=’\n’为啥不能直接写a啊

盛夏光年1999
def main():
    str_input = input()
    count = {}
    str_not_split = str_input.replace(" ", "")
    str_length = len(str_not_split)
    for i in set(str_not_split):
        count[i] = str_not_split.count(i)
    count_result = sorted(count.items(), key=lambda x: (-x[1], x[0]))
    max_value = count_result[0][1]
    print('%c %.2f' % (count_result[0][0],max_value / str_length))


if __name__ == '__main__':
    main()
13627999316

会用STL就行了

#include <bits/stdc++.h>
#include <algorithm>
using namespace std;

map<char,int> mp;
set<char> se;
int main() {
    string s;
    getline(cin,s); 
    int cnt = 0;    
    for(int i = 0;i < s.length();i++) {
        if(s[i]!=' '){
            cnt++;
            mp[s[i]]++;
        }
    }

    int max = -1;
    for(auto it = mp.begin();it != mp.end();it++) {
        int count = it->second;
        if(count > max) max = count;
    }

    for(auto it = mp.begin();it != mp.end();it++) {
        if(it->second == max) {
            se.insert(it->first);
        }
    }

    auto it = se.begin();
    printf("%c %.2lf",*it,(double)mp[*it]/cnt);
    return 0;
}
CCXXXI_
[已删除]
Li Dao

一种粗鲁的写法

include

using namespace std;
int ci[300],tot=0;
int amax=0;
char maxch;
int main()
{
memset(ci,0,sizeof(ci));
char tmp;

while((tmp=getchar())!=EOF)
if(isalpha(tmp)) {ci[tmp]++;tot++;if(ci[tmp]>amax) {amax=ci[tmp];maxch=tmp;}}
printf(“%c %.2f\n”,maxch,1.0*amax/tot);
return 0;
}

如果次数数组不用拼音,只开26个元素,就很优雅了

Saitama
#include <bits/stdc++.h>

using namespace std;

typedef struct alpha{char c; int t = 0; int sub;}alpha;

bool cmp(const alpha &x, const alpha &y)
{
    if(x.t == y.t)
        return x.sub < y.sub;
    else
        return x.t > y.t;
}

int main()
{
    string s;
    getline(cin, s);
    vector <alpha> a(26);
    int space = 0;
    for(int i = 0; i < s.size(); i++)
    {
        if(s[i] == ' ')
        {
            space++;
            continue;
        }
        a[s[i] - 'a'].sub = s[i] - 'a';
        a[s[i] - 'a'].t++;
    }
    sort(a.begin(), a.end(), cmp);
    int len = s.size() - space;
    double ans = a[0].t * 1.0 / len;
    printf("%c %.2lf\n", a[0].sub + 'a', ans);
    return 0;
}
10175101245

不得不说,用python刷这种简单的实现题真的爽……

s = input()
counts = dict()
for c in [chr(i) for i in range(97,123)]:
counts[c] = s.count(c)
max_char = max(counts, key = counts.get)
print(‘%c %.2f’ % (max_char,counts[max_char]/(len(s) - s.count(‘ ‘))))

徐摆渡

写的比较复杂,但是方法比较笨,当作多关键字排序写的

include

include

include

//2852 to count the frequency of a letter. multiple key element sorting
struct Word
{
char ch;
int times;
double possibility;
}word[26];

int cmp(const void a,const void b)
{
struct Word x=(struct Word) a;
struct Word y=(struct Word) b;
if(x->possibilitypossibility) return 1;
else if(x->possibility==y->possibility&&x->ch>y->ch) return 1;
else return -1;
}

int main()
{
char str[4000];
char text[3000];
gets(str);
int i,j=0;
int len1=strlen(str);
for(i=0;i<len1;i++)
if(str[i]!=’ ‘) text[j++]=str[i];//removal of space char
int len2=j;

for(i=0;i<26;i++){
    word[i].times=1;
    word[i].possibility=0.0;
}//structure initialization

int l=0;
for(i=0;i<len2;i++)
    if(text[i]!='0'){
        word[l].ch=text[i];
        for(j=i+1;j<len2;j++)
            if(text[j]==text[i]) {
                word[l].times++;
                text[j]='0';
            }
        l++;
    }//input of key element

int len3=l;

for(i=0;i<len3;i++) word[i].possibility=(double)word[i].times/len2;
qsort(word,len3,sizeof(word[0]),cmp);
printf("%c %.2lf",word[0].ch,word[0].possibility);
return 0;

}

你当前正在回复 博客/题目
存在问题!