uva 10008 What's Cryptanalysis?

本文介绍了一个简单的密码分析程序设计思路,该程序能够分析给定文本中英文字母的出现频率,并按频率从高到低输出所有出现过的字母。程序通过统计输入文本中各个字母的出现次数,忽略非字母字符,实现了一种基本的密码分析功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cryptanalysis is the process of breaking someone else’s cryptographic writing. This sometimes involves some kind of statistical analysis of a passage of (encrypted) text. Your task is to write a program which performs a simple analysis of a given text.
Input
The first line of input contains a single positive decimal integer n. This is the number of lines which follow in the input. The next n lines will contain zero or more characters (possibly including whitespace). This is the text which must be analyzed.
Output
Each line of output contains a single uppercase letter, followed by a single space, then followed by a positive decimal integer. The integer indicates how many times the corresponding letter appears in the input text. Upper and lower case letters in the input are to be considered the same. No other characters must be counted. The output must be sorted in descending count order; that is, the most frequent letter is on the first output line, and the last line of output indicates the least frequent letter. If two letters have the same frequency, then the letter which comes first in the alphabet must appear first in the output. If a letter does not appear in the text, then that letter must not appear in the output.
Sample Input
3
This is a test.
Count me 1 2 3 4 5.
Wow!!!! Is this question easy?
Sample Output
S 7
T 6
I 5
E 4
O 3
A 2
H 2
N 2
U 2
W 2
C 1
M 1
Q 1
Y 1

题意:
输入n行字符串,按出现次数从大到小输出字符串中出现的所有英文字母,若两个字母出现次数一样,则按照字典序输出

#include <iostream>
#include<cstdio>
using namespace std;
int main()
{
    int N,i=0,k,max=0,n;
    char ch;
    int num[26]={0};
    string str,line[100];
    cin>>N;
    getchar();
    for(i=0;i<N;i++)
    {
        getline(cin,str);
        line[i]=str;
    }
    for(i=0;i<N;i++)
    {
        for(k=0;line[i][k]!='\0';k++)
        {
            ch=line[i][k];
            if(ch>='a'&&ch<='z')//将小写字母转化为大写
            {
                ch=ch-32;
            }
            if(ch>='A'&&ch<='Z')
            {
                num[ch-'A']++;//下标代表每个出现的字母,数组中储存每个字母的出现次数
            }
        }
    }
    while(1)//循环找出当前数组中最大值,即出现次数最多的字母
    {
        for(k=0;k<26;k++)
        {
            if(num[k]>max)
            {
                max=num[k];//记录最大值
                n=k;//记录下标
            }
        }
        if(max!=0)
        {
        ch='A'+n;//将下标转为相应字母
        cout<<ch<<" "<<max<<endl;
        num[n]=0;
        max=0;
        }
        else
            break;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值