[解题报告]10008 What's Cryptanalysis?

  What's Cryptanalysis? 

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

Miguel Revilla 
2000-08-22
 
大致分为统计和排序两块
 
既然每个字母都有自己的出现次数,那么就用结构体把字符型和统计数整型进行绑定
 
struct zftj
{
     char ch;
     int num;
 }a[26];

赋初值

for(i=0;i<26;i++)
     {
         a[i].ch=i+'a';
         a[i].num=0;
     }

统计,如果是大写,转换成小写再统计

while((c=getchar())!=EOF)
     {
         if(c>='A'&&c<='Z')
             c+=32;
         if(c>='a'&&c<='z')
             a[c-'a'].num++;
     }

 前后比较并排序,如果后一个字母次数多,字母提前,统计数对应提前,如果统计数一样,根据ASC2码排序(也就是字母表顺序)

for(i=0;i<26;i++)
         for(j=i+1;j<26;j++)
         {
             if(a[i].num<a[j].num)
             {
                 t=a[i].num;
                 a[i].num=a[j].num;
                 a[j].num=t;
                 temp=a[i].ch;
                 a[i].ch=a[j].ch;
                 a[j].ch=temp;
             }
             if(a[i].num==a[j].num)
                 if(a[i].ch>a[j].ch)
                 {
                     t=a[i].num;
                     a[i].num=a[j].num;
                     a[j].num=t;
                     temp=a[i].ch;
                     a[i].ch=a[j].ch;
                     a[j].ch=temp;
                 }
         }

AC代码

#include <stdio.h>
 struct zftj
 {
     char ch;
     int num;
 }a[26];
 int main()
 {
     int n, i, j, t;
     char c,temp;
     scanf("%d",&n);
     while(n--)
     {
     for(i=0;i<26;i++)
     {
         a[i].ch=i+'a';
         a[i].num=0;
     }
     while((c=getchar())!=EOF)
     {
         if(c>='A'&&c<='Z')
             c+=32;
         if(c>='a'&&c<='z')
             a[c-'a'].num++;
     }
     for(i=0;i<26;i++)
         for(j=i+1;j<26;j++)
         {
             if(a[i].num<a[j].num)
             {
                 t=a[i].num;
                 a[i].num=a[j].num;
                 a[j].num=t;
                 temp=a[i].ch;
                 a[i].ch=a[j].ch;
                 a[j].ch=temp;
             }
             if(a[i].num==a[j].num)
                 if(a[i].ch>a[j].ch)
                 {
                     t=a[i].num;
                     a[i].num=a[j].num;
                     a[j].num=t;
                     temp=a[i].ch;
                     a[i].ch=a[j].ch;
                     a[j].ch=temp;
                 }
         }
     for(i=0;i<26;i++)
         if(a[i].num>0)
             printf("%c %d\n",a[i].ch-32,a[i].num);
     }
     return 0;
 }
原文地址:https://www.cnblogs.com/TheLaughingMan/p/2919889.html