成绩排名 c++
讀入 n(>0)名學生的姓名、學號、成績,分別輸出成績最高和成績最低學生的姓名和學號。
輸入格式:
每個測試輸入包含 1 個測試用例,格式為
第 1 行:正整數 n
第 2 行:第 1 個學生的姓名 學號 成績
第 3 行:第 2 個學生的姓名 學號 成績
? ... ... ...
第 n+1 行:第 n 個學生的姓名 學號 成績
其中姓名和學號均為不超過 10 個字符的字符串,成績為 0 到 100 之間的一個整數,這里保證在一組測試用例中沒有兩個學生的成績是相同的。
輸出格式:
對每個測試用例輸出 2 行,第 1 行是成績最高學生的姓名和學號,第 2 行是成績最低學生的姓名和學號,字符串間有 1 空格。
輸入樣例:
3 Joe Math990112 89 Mike CS991301 100 Mary EE990830 95輸出樣例:
Mike CS991301
Joe Math990112
實現:
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;//建立一個學生類 含有學生姓名 學生學號 學生成績
class Student
{
public:string s_name;string s_id;int s_goal;//構造函數 Student(string name, string id, int goal) :s_name(name), s_id(id), s_goal(goal) {};};//加入一個比較函數cop() 按引用的方式傳遞
bool cmp(Student& s1, Student& s2)
{return s1.s_goal > s2.s_goal;
}int main()
{int line;cin >> line;//應用vector容器 存儲每一個學生數據vector<Student>stu;for (int i = 0; i < line; i++){string name;string id;int age;cin >> name >> id >> age;Student s(name, id, age);stu.push_back(s);//插入學生數據}//排序sort(stu.begin(), stu.end(), cmp);cout << stu[0].s_name << " " << stu[0].s_id << endl;cout << stu[line-1].s_name << " " << stu[line-1].s_id << endl;return 0;}
題目來源:PAT (Basic Level) Practice
總結
- 上一篇: 防止asp马后门
- 下一篇: Mathorcup数学建模竞赛第三届-【