统计英文字母出现的频率

题目描述

  字母出现频率是指26英文个字母在文章中出现的频率。根据统计,在英语中最常出现的字母是e,大约占12~13%,出现最少的字母是z,不到0.1%  ,如下图所示(统计结果来自wiki百科)

4dd751c7087f27402e85e78df5d42532.png



字母出现频率的统计结果可用于破解密码,例如基于字母移位替换的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;

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值