PAT乙級1004:成績ランキング(20)
タイトル:
n名の学生の名前、学号、成績を読み込み、成績が最も高い学生と成績が最も低い学生の名前と学号をそれぞれ出力する.
入力フォーマット:各テスト入力には1つのテスト例が含まれ、フォーマットは
出力フォーマット:テスト例ごとに2行を出力し、1行目は成績が最も高い学生の名前と学番、2行目は成績が最も低い学生の名前と学番で、文字列の間に1スペースがあります.
サンプルを入力:
出力サンプル:
構造体を定義し、各学生の名前と成績を配置し、成績の大きい順に並べ替えます.成績の最大と最小の名前と学号を出力します.
私のソース:
n名の学生の名前、学号、成績を読み込み、成績が最も高い学生と成績が最も低い学生の名前と学号をそれぞれ出力する.
入力フォーマット:各テスト入力には1つのテスト例が含まれ、フォーマットは
1 : n
2 : 1
3 : 2
... ... ...
n+1 : n
名前と学号はいずれも10文字を超えない文字列であり、成績は0から100の間の整数であり、ここでは1組の試験例で2人の学生の成績が同じではないことを保証する.出力フォーマット:テスト例ごとに2行を出力し、1行目は成績が最も高い学生の名前と学番、2行目は成績が最も低い学生の名前と学番で、文字列の間に1スペースがあります.
サンプルを入力:
3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95
出力サンプル:
Mike CS991301
Joe Math990112
構造体を定義し、各学生の名前と成績を配置し、成績の大きい順に並べ替えます.成績の最大と最小の名前と学号を出力します.
私のソース:
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct Student{
string name;
string ID;
int score;
};
bool cmp(Student A, Student B){
return A.score > B.score;
}
class ScoreSort{
public:
void Ans(){
const int MAXN = 1010;
int n;
cin >> n;
Student a[MAXN];
for (int i = 0; i < n; i++){
cin >> a[i].name >> a[i].ID >> a[i].score;
}
sort(a, a+n, cmp);
cout << a[0].name << " " << a[0].ID << endl;
cout << a[n-1].name << " " << a[n-1].ID << endl;
}
};
int main(){
ScoreSort solution;
solution.Ans();
return 0;
}