当前位置:编程学习 > C/C++ >>

(字符串的模式匹配4.7.8)UVA 10008 What's Cryptanalysis?(统计文本中某一个字符的出现的次数,需要从小到大排序&&同次数的按字

 
  
 
/* 
 * UVA_10008.cpp 
 * 
 *  Created on: 2013年10月26日 
 *      Author: Administrator 
 */  
  
#include <iostream>  
#include <stdio.h>  
#include <algorithm>  
#include <string.h>  
  
  
using namespace std;  
  
struct inf{  
    int num, id;  
}a[30];  
  
bool cmp(inf x, inf y) {  
    if(x.num != y.num) return x.num > y.num;  
    return x.id < y.id;  
}  
  
  
int main(){  
    int n;  
    char s[1000];  
    while(scanf("%d",&n)!=EOF){  
        int i,j;  
        for(i = 0 ; i < 26 ; ++i){  
            a[i].id = i;  
            a[i].num = 0;  
        }  
  
        gets(s);  
//      scanf("%s",&s);  
        for(i = 0 ; i < n ; ++i){  
  
//          scanf("%s",&s);  
            gets(s);  
            for(j = 0 ; j < strlen(s);++j){  
                if(isalpha(s[j])){//判断该字符是否是字母,排除空格符及?号等情况  
                    a[tolower(s[j]) - 'a'].num++;//tolower(s[j])将所有字符都穿换成小写  
                }  
            }  
        }  
  
        sort(a,a+26,cmp);  
  
        for(i = 0 ; i < 26 ; ++i){  
            if(a[i].num !=0){  
  
                printf("%c %d\n",(a[i].id +'A'),a[i].num);  
            }  
        }  
    }  
  
    return 0;  
}  

 

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,