題目描述:
對於在中國大學MOOC(http://www.icourse163.org/ )學習“數據結構”課程的學生,想要獲得一張合格證書,總評成績必須達到 60 分及以上,並且有另加福利:總評分在 [G, 100] 區間內者,可以得到 50 元 PAT 代金券;在 [60, G) 區間內者,可以得到 20 元PAT代金券。全國考點通用,一年有效。同時任課老師還會把總評成績前 K 名的學生列入課程“名人堂”。本題就請你編寫程序,幫助老師列出名人堂的學生,並統計一共發出了面值多少元的 PAT 代金券。
輸入格式:
輸入在第一行給出 3 個整數,分別是 N(不超過 10 000 的正整數,爲學生總數)、G(在 (60,100) 區間內的整數,爲題面中描述的代金券等級分界線)、K(不超過 100 且不超過 N 的正整數,爲進入名人堂的最低名次)。接下來 N 行,每行給出一位學生的賬號(長度不超過15位、不帶空格的字符串)和總評成績(區間 [0, 100] 內的整數),其間以空格分隔。題目保證沒有重複的賬號。
輸出格式:
首先在一行中輸出發出的 PAT 代金券的總面值。然後按總評成績非升序輸出進入名人堂的學生的名次、賬號和成績,其間以 1 個空格分隔。需要注意的是:成績相同的學生享有並列的排名,排名並列時,按賬號的字母序升序輸出。
輸入樣例:
10 80 5 [email protected] 78 [email protected] 87 [email protected] 65 [email protected] 96 [email protected] 39 [email protected] 87 [email protected] 80 [email protected] 88 [email protected] 80 [email protected] 70
輸出樣例:
360 1 [email protected] 96 2 [email protected] 88 3 [email protected] 87 3 [email protected] 87 5 [email protected] 80 5 [email protected] 80
#include <iostream>
#include <algorithm>
using namespace std;
struct node {
string name;
int pat;
};
bool cmp(node a, node b){ // 定義排序規則
if (a.pat == b.pat) // 成績相同,按賬號升序排列
return a.name < b.name;
return a.pat > b.pat; // 否則,按成績排序
}
node a[10002];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n, g, k;
int sum = 0;
cin >> n >> g >> k;
for (int i = 1; i <= n; i++) { // 輸入
cin >> a[i].name >> a[i].pat;
if (a[i].pat >= g)
sum += 50;
else if (a[i].pat >= 60)
sum += 20;
}
sort(a+1, a+1+n, cmp); // 排序
int r = 1; // 名次
cout << sum << endl; // 輸出PAT 代金券的總面值
// 爲了下面 if(a[i].pat != a[i-1].pat)的a[i-1]才單獨輸出
cout << r << " " << a[1].name << " " << a[1].pat << endl;
for (int i = 2; i <= n; i++) {
if (a[i].pat != a[i-1].pat) // 並排名次
r = i;
if (r > k) // 輸出已超過 k 名, 結束
break;
cout << r << " " << a[i].name << " " << a[i].pat << endl;
}
return 0;
}