题目描述
字母出现频率是指26英文个字母在文章中出现的频率。根据统计,在英语中最常出现的字母是e,大约占12~13%,出现最少的字母是z,不到0.1% ,如下图所示(统计结果来自wiki百科)
字母出现频率的统计结果可用于破解密码,例如基于字母移位替换的ROTn密码(又称为恺撒密码);在很多压缩工具中,也要统计字母(或字符、单词)出现的频率,以便用更少的二进制位存储出现频率较高的字母(字符、单词),从而减少数据占用的存储空间
输入
输入一段文章,字数少于10000,统计文章中字母A-Z出现的频率(不区分大小写,忽略非字母的符号:数字、标点、换行...)
输出
统计结果按百分数显示,保留两位小数
样例输入 Copy
Introduction to The Junior College of Zhejiang Wanli University On Huilong Campus, in order to cater to and meet the particular requirements of freshmen, the university implements independent management and places emphasis on fundamental teaching of foreign languages and computer skills. The course credits, flexible teaching, tutorial system and teaching according to their aptitude, hence student-oriented management system is realized.
样例输出 Copy
A: 7.26% B: 0.27% C: 4.03% D: 4.03% E:13.71% F: 1.88% G: 3.23% H: 3.49% I: 8.06% J: 0.54% K: 0.27% L: 4.03% M: 4.03% N: 9.68% O: 5.65% P: 2.15% Q: 0.27% R: 5.65% S: 5.38% T: 9.68% U: 4.03% V: 0.54% W: 0.27% X: 0.27% Y: 1.08% Z: 0.54%
#include <stdio.h>
#include <string.h>
char str[300];
int a[10000];
int main() {
int cnt = 0;
char c;
while(~scanf("%c" , &c)) {
if(c >= 'A' && c <= 'Z') c += 32;
if(c >= 'a' && c <= 'z') {
a[c] ++;
cnt ++;
}
}
int i = 'A' , j = 'a';
if(cnt == 0) {
double res = 0.000;
for(;j <= 'z'; j ++ , i ++) { printf("%c:%5.2lf%%" , i , res);
puts("");
}
return 0;
}
for(;j <= 'z'; j ++ , i ++) {
printf("%c:%5.2lf%%" , i , (a[j] * 1.0 / (cnt * 1.0) * 100));
puts("");
}
return 0;
}