1182.奖学金
Description
Input
Output
Sample Input
6 90 67 80 87 66 91 78 89 91 88 99 77 67 89 64 78 89 98
Sample Output
6 265 4 264 3 258 2 244 1 237
Source
© 2012-2015 SDNU ACM-ICPC TEAM, v1.2 Beta
首先看到这个排序题,第一需要想到sort函数(基本上是最快的排序函数)。但是要注意这次需要是降序输出,而sort函数默认是升序输出,所以这就需要我们去用冒泡来自定义sort函数,修改为降序输出。然后就是简单的输入、排序、输出,用两个for循环就可以解决。这个题就这样做完了!
这是我第一次接触结构体排序,对于一些知识还不是很熟悉,所以我也没有写的很细。等以后我熟练掌握结构体排序之后,再写一个详细的。
#include <stdio.h>
#include<algorithm>
using namespace std;
struct student
{
int sum;
int chi;
int mat;
int eng;
int num;
}stu [310];
int i;
bool cmp(student a,student b)
{
if(a.sum>b.sum)
return 1;
else if (a.sum<b.sum)
return 0;
else
{
if(a.chi>b.chi)
return 1;
else if (a.chi<b.chi)
return 0;
else
{
if(a.num<b.num)
return 1;
else
return 0;
}
}
}
int main ()
{
int n;
scanf ("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d %d %d",&stu[i].chi,&stu[i].mat,&stu[i].eng);
stu[i].sum=stu[i].chi+stu[i].mat+stu[i].eng;
stu[i].num=i+1;
}
sort (stu,stu+n,cmp);
for(i=0;i<5;i++)
printf("%d %d\n",stu[i].num,stu[i].sum);
return 0;
}
这个我感觉需要注意的有以下几点:
1、定义结构体内部的参量以及s【310】;
2、注意写对代码,尤其是那些点“.”;
3、自定义的排序函数,判断大小后的函数返回值一定要根据排序方式来定。