UVA 10008 whats cryptanalysis 水题

来源:互联网 发布:贵州省大数据局电话 编辑:程序博客网 时间:2024/06/16 10:09

这道题如果用纯c代码和冒泡排序就可以解决了   冒泡排序是稳定排序  对于只有26个对象的数组排序也是非常的快  时间也许比快排 堆排 归并要小  毕竟排序数小

如果用到c++代码和排序算法来写就要复杂一些  排序算法也要考虑使用是稳定排序的算法   但是这完全是没必要的   因为我在学习c++所以蛋疼的用了c++

只对这道题来说  第一种方法最简单  代码也简洁   但是从长远来看  熟悉c++的输入输出和稳定排序算法还是很有必要的   stable_sort是stl中的归并排序算法  而sort是快排  不稳定排序算法。

#include<iostream>#include<cstring>#include<algorithm>#include<cctype>using namespace std;//1:动态申请内存 创建结构体或者array 输入字符串用getline(cin,地址)2:统计输入字符 用cctype// 3:对结构体排序 对array对象数组排序 4:输出const int maxn = 65535;struct character{    char c;    int n;}ch[26];int cmp1(character a, character b){    return a.n > b.n;}int cmp2(character a, character b){    return a.c > b.c;}int main(){    int n;    while(cin>>n)    {        for(int i = 0; i < 26; i++)//init ch        {            ch[i].c = 'A'+i;            ch[i].n = 0;        }        cin.get();        for(int i = 0; i < n; i++)        {            string str;//char *str = new char[maxn];            getline(cin,str);            for(int k = 0; str[k]; k++)            {                if((str[k]>='a'&&str[k]<='z')||(str[k]>='A'&&str[k]<='Z'))                    {                        char s = str[k];                        s = toupper(s);                        ch[s-'A'].n++;                    }            }            //delete [] str;        }        stable_sort(ch,ch+25,cmp1);        //sort(ch,ch+25,cmp2);        for(int i = 0 ; i < 26; i++)            if(ch[i].n != 0)            cout<<ch[i].c<<" "<<ch[i].n<<endl;    }    return 0;}


0 0
原创粉丝点击