題目連結: https://zerojudge.tw/ShowProblem?problemid=c012
# 解題思路
用 struct 紀錄,在自訂排序後,輸出即可!!!
# 程式碼
#include <bits/stdc++.h> | |
using namespace std; | |
typedef long long ll; | |
typedef struct _node{ | |
int ascii; | |
int count; | |
} Node; | |
bool compare (Node a,Node b){ | |
if (a.count == b.count) return b.ascii < a.ascii; | |
else return a.count < b.count; | |
} | |
int main () | |
{ | |
string s; | |
int first=1; | |
while (getline(cin,s)){ | |
if (first == 1){ | |
first=0; | |
}else { | |
cout << endl; | |
} | |
int c[257]={0}; | |
Node mynode[257]; | |
for(int i=0;i<s.length();i++){ | |
c[s[i]]++; | |
} | |
int k=0; | |
for(int i=0;i<257;i++){ | |
if (c[i]>0){ | |
mynode[k].ascii=i; | |
mynode[k].count=c[i]; | |
k++; | |
} | |
} | |
sort(mynode,mynode+k,compare); | |
for(int i=0;i<k;i++) | |
cout << mynode[i].ascii << " "<<mynode[i].count <<endl; | |
} | |
} |