1004成績排名(20分)
題目
讀入 n(>0)名學生的姓名、學號、成績,分別輸出成績最高和成績最低學生的姓名和學號
輸入格式
每個測驗輸入包含 1 個測驗用例,格式為:

其中姓名和學號均為不超過 10 個字符的字串,成績為 0 到 100 之間的一個整數,這里保證在一組測驗用例中沒有兩個學生的成績是相同的,
輸出格式:
對每個測驗用例輸出 2 行,第 1 行是成績最高學生的姓名和學號,第 2 行是成績最低學生的姓名和學號,字串間有 1 空格,
輸入樣例:

輸出樣例:

代碼
這里我是用vector寫的,代碼如下:
#include"bits/stdc++.h"
using namespace std;
struct peo{
int score;
string id;
string name;
};
bool cmp(peo a, peo b)
{
return a.score > b.score;
}
int main()
{
int n;
vector<peo>v;
cin >> n;
for(int i = 0; i < n; i++)
{
string name, id;
int score;
cin >> name >> id >> score;
peo t;
t.id = id;
t.name = name;
t.score = score;
v.push_back(t);
}
sort(v.begin(), v. end(), cmp);
cout << v[0].name << " " << v[0].id << endl;
cout << v[n-1].name << " " << v[n-1].id << endl;
system("pause");
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/258086.html
標籤:其他
