CCF认证真题-(201503-2)-数字排序
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 5 struct pairs{ 6 int index; 7 int times; 8 }; 9 pairs arr[1005]; 10 11 bool cmp (pairs a, pairs b) { 12 if (a.times == b.times) 13 return a.index < b.index; 14 return a.times > b.times; 15 } 16 17 int main() 18 { 19 ios::sync_with_stdio(false); 20 cin.tie(0); 21 22 int n; 23 cin >> n; 24 int cnt = 0; 25 for (int i = 0; i < 1005; i++) 26 arr[i].index = i; 27 28 for (int i = 0; i < n; i++) { 29 int x; 30 cin >> x; 31 if (arr[x].times == 0) 32 cnt++; 33 arr[x].times++; 34 } 35 sort(arr, arr + 1005, cmp); 36 37 for (int i = 0; i < cnt; i++) 38 cout << arr[i].index << ' ' << arr[i].times << endl; 39 40 return 0; 41 }
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 5 struct Node { 6 int num; 7 int time; 8 Node() {num = -1; time = 0;} 9 }; 10 Node arr[1005]; 11 12 bool cmp(Node a, Node b) 13 { 14 return (a.time > b.time) || (a.time == b.time && a.num < b.num); 15 } 16 17 int main() 18 { 19 ios::sync_with_stdio(false); 20 cin.tie(0); 21 int n; 22 cin >> n; 23 for (int i = 1; i <= n; i++) { 24 int x; 25 cin >> x; 26 arr[x].num = x; 27 arr[x].time++; 28 } 29 sort(arr, arr + 1004, cmp); 30 for (int i = 0; i <= 1005; i++) { 31 if (arr[i].num == -1 || arr[i].time == 0) break; 32 cout << arr[i].num << ' ' << arr[i].time << endl; 33 } 34 35 return 0; 36 }