题解 UVA10008 【What's Cryptanalysis?】
很水的一道题!
注意:
-
全部转换成大写
-
只有大写字母才算
-
要排序,所以封装成类(结构体也可以)
代码:
#include <iostream>
#include <cctype>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
class Node
{
public:
int f, x;
Node();
};
Node::Node()
{
x = f = 0;
}
inline bool cmp(const Node& x, const Node& y)
{
return (x.x == y.x ? x.f < y.f : x.x > y.x);
}
Node a[30];
int main()
{
int n, cur = 0;
cin >> n;
getchar();
for (register int i = 1; i <= n; i++)
{
string s;
getline(cin, s);
int len = s.length() - 1;
transform(s.begin(), s.end(), s.begin(), ::toupper);
for (register int j = 0; j <= len; j++)
{
if (s[j] >= 'A' && s[j] <= 'Z')
{
a[s[j] - 'A' + 1].x++;
a[s[j] - 'A' + 1].f = int(s[j]);
}
}
}
sort(a + 1, a + 30, cmp);
for (register int i = 1; i <= 26; i++)
{
if (a[i].x == 0 || a[i].f == 0)
{
continue;
}
else
{
cout << char(a[i].f) << " " << a[i].x << endl;
}
}
//system("pause");
return 0;
}

浙公网安备 33010602011771号