今天是練習cpe的Tell me the frequencies題目
#include <bits/stdc++.h>
#define p pair<int,int>
using namespace std;
bool cmp(p a, p b)
{
if (a.first != b.first)
{
return a.first < b.first;
}
else
{
return a.second > b.second;
}
}
int main()
{
string s;
while (getline(cin, s))
{
p a[256];
for (int i = 0; i < 256; i++)
{
a[i] = {0, i};
}
for (int i = 0; i < s.length(); i++)
{
a[(int)s[i]].first++;
}
sort(a, a+256, cmp);
for (auto i: a)
{
if (i.first > 0)
{
cout << i.second << " " << i.first << endl ;
}
}
cout << endl ;
}
return 0;
}