UVA 10008 whats cryptanalysis 水题

这道题如果用纯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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值