網(wǎng)站建設(shè)那個(gè)好點(diǎn)擊seo軟件
查找和排序
題目:輸入任意(用戶,成績)序列,可以獲得成績從高到低或從低到高的排列,相同成績
都按先錄入排列在前的規(guī)則處理。
示例:
jack??????70
peter?????96
Tom???????70
smith?????67
從高到低??成績
peter?????96
jack??????70
Tom???????70
smith?????67
從低到高
smith?????67
jack? ? ? 70
Tom? ? ? 70
peter?????96
?
#include<iostream>
#include <iomanip>
#include <algorithm>using namespace std;typedef struct student {string name;int grade;int squeue;
} student;bool compare1(student a, student b) {if (a.grade != b.grade) {return a.grade < b.grade;} else {return a.squeue < b.squeue;}}bool compare2(student a, student b) {if (a.grade != b.grade) {return a.grade > b.grade;} else {return a.squeue < b.squeue;}
}int main() {int n = 0, way = 0;while (::scanf("%d %d", &n, &way) != EOF) {student cla[n];for (int i = 0; i < n; ++i) {cin >> cla[i].name >> cla[i].grade;cla[i].squeue=i;}if (way == 1) {sort(cla, cla + n, compare1);} else {sort(cla, cla + n, compare2);}for (int i = 0; i < n; ++i) {cout << cla[i].name << " " << cla[i].grade << endl;}}return 0;}
?這道題總體不難,只有一個(gè)需要注意的細(xì)節(jié),只要成績相同就按照先后順序來填,所以在編寫compare1和compare2函數(shù)的時(shí)候,對squeue的比較代碼是相同的
?