【洛谷】P1051 谁拿了最多奖学金 【题解】
这一题呢由于输入数据非常多,所以我们需要用到struct结构体来解题~,由于奖学金的种类很多,所以代码也比较长,大家忍受一下!
蒟蒻打的手都酸了
这一题我们需要注意,这些奖学金是可以叠加的,所以当第一个奖学金判断完后,后面的判断不能用else if,还是要用if,不然很无辜的几位同学就会莫名其妙的少掉一堆奖学金了!
上代码!注:我命名太长了,写得惨不忍睹 ,大家谅解一下~
#include<iostream>
#include<algorithm>
using namespace std;
struct student{
string name;
int averageScore;
int deliberatingScore;
char studentCadres;
char westStudent;
int papersNumber;
int money;
int serialNumber;
};
bool cmp(student a,student b){
if(a.money==b.money)
return a.serialNumber<b.serialNumber;
return a.money>b.money;
}
int main(){
student student[1001];
int n,num=0;cin>>n;
for(int i=1;i<=n;i++){
cin>>student[i].name;
cin>>student[i].averageScore;
cin>>student[i].deliberatingScore;
cin>>student[i].studentCadres;
cin>>student[i].westStudent;
cin>>student[i].papersNumber;
student[i].serialNumber=i;
student[i].money=0;
if(student[i].averageScore>80&&student[i].papersNumber>0)
student[i].money+=8000;
if(student[i].averageScore>85&&student[i].deliberatingScore>80)
student[i].money+=4000;
if(student[i].averageScore>90)
student[i].money+=2000;
if(student[i].westStudent=='Y'&&student[i].averageScore>85)
student[i].money+=1000;
if(student[i].studentCadres=='Y'&&student[i].deliberatingScore>80)
student[i].money+=850;
num+=student[i].money;
}sort(student+1,student+n,cmp);
cout<<student[1].name<<endl<<student[1].money<<endl<<num;
return 0;
}
这一题毕竟是结构体排序,而且代码本来就很长,所以这里用了sort排序,如果用冒泡和选择的话,代码就更惨不忍睹了~
对了,最后提醒大家,结构体排序如果用sort,一定要写cmp!
end~