#32031: c++


xig1517 (超級小蛇)

學校 : 元智大學
編號 : 142909
來源 : [140.138.224.30]
最後登入時間 :
2023-12-27 20:25:18
c044. 10008 - What's Cryptanalysis -- UVa10008 | From: [36.224.87.9] | 發表日期 : 2022-09-09 00:24

#include <bits/stdc++.h>
using namespace std;
#define AND &&
#define OR ||

struct dictionary {
    char ch;
    int num;
} dic[50] = {{.ch = '\0', .num=0}};

bool comp (dictionary a, dictionary b) {
    if (a.num == b.num) return a.ch<b.ch;
    else return a.num>b.num;
}

main ()
{
    cin.tie(0); cout.sync_with_stdio(false);
    int N; cin>>N;
    N++;
    while (N--){
        string str; getline(cin, str);
        for (int i=0; i<str.length(); i++) {
            if (str[i] >= 'A' AND str[i] <= 'Z') {
                dic[str[i]-'A'].num ++;
                dic[str[i]-'A'].ch = str[i];
            }else if(str[i] >= 'a' AND str[i] <= 'z'){
                dic[str[i]-'a'].num ++;
                dic[str[i]-'a'].ch = str[i]-'a'+'A';
            }
            else continue;
        }
    }
    sort(dic, dic+50, comp);
    for (int i=0; i<50; i++) {
        if (dic[i].num == 0) break;
        cout<<dic[i].ch<<" "<<dic[i].num<<endl;
    }
}

 

N++是因為getline會直接吃到N輸入中的換行符號'\n'
所以會直接跳掉

 
ZeroJudge Forum