1. 程式人生 > >B.1004 成績排名

B.1004 成績排名

讀入n名學生的姓名,學號,成績,輸出成績最高和最低的姓名和學號

輸入格式:3

                 aaa  math980   90

                 bbb  cs9998      89

                 ccc   ds9098     70

輸出格式:            aaa  math980   90

                              ccc   ds9098     70

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
struct student{
	 string name;
	 string id; 
	 int score;
}a[101];
int comp (const student &a,const student &b){
	return a.score> b.score;
}
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i].name>>a[i].id>>a[i].score;
	}
	sort(a,a+n,comp);
	cout<<a[0].name<<" "<<a[0].id<<endl;
	cout<<a[n-1].name<<" "<<a[n-1].id;
}

學習結構體的使用,這個程式還有小bug== ;

主要是注意comp的形參定義,防止報錯;