清華大學 上機複試 成績排序 簡單排序
阿新 • • 發佈:2018-12-02
思路:就是簡單的用兩個自定義比較函式,還有一點就是輸入是多組輸入,要while(scanf()!=EOF)
#include<bits/stdc++.h> using namespace std; struct St { int sco; int place; char name[20]; }stu[200]; bool cmp1(St a, St b) { if (a.sco == b.sco) return a.place < b.place; return a.sco < b.sco; } bool cmp2(St a, St b) { if (a.sco == b.sco) return a.place < b.place; return a.sco > b.sco; } int main() { int n, m; while (scanf("%d%d", &n, &m) != EOF) { for (int i = 0; i < n; i++) { scanf("%s %d", &stu[i].name, &stu[i].sco); stu[i].place = i + 1; } if (m == 0) sort(stu, stu + n, cmp2); else sort(stu, stu + n, cmp1); for (int i = 0; i < n; i++) printf("%s %d\n", stu[i].name, stu[i].sco); } //system("pause"); return 0; }