题目描述
某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金。期末,每个学生都有3门课的成绩:语文、数学、英语。先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规定学号小的同学 排在前面,这样,每个学生的排序是唯一确定的。
任务:先根据输入的3门课的成绩计算总分,然后按上述规则排序,最后按排名顺序输出前五名名学生的学号和总分。注意,在前5名同学中,每个人的奖学金都不相同,因此,你必须严格按上述规则排序。例如,在某个正确答案中,如果前两行的输出数据(每行输出两个数:学号、总分) 是:
77 279279
55 279279
这两行数据的含义是:总分最高的两个同学的学号依次是77号、55号。这两名同学的总分都是 279279 (总分等于输入的语文、数学、英语三科成绩之和) ,但学号为77的学生语文成绩更高一些。如果你的前两名的输出数据是:
55 279279
77 279279
则按输出错误处理,不能得分。
输入格式
共n+1行。
第11行为一个正整数n( \le 300)n(≤300),表示该校参加评选的学生人数。
第22到n+1n+1行,每行有33个用空格隔开的数字,每个数字都在00到100100之间。第jj行的33个数字依次表示学号为j-1j−1的学生的语文、数学、英语的成绩。每个学生的学号按照输入顺序编号为1~n1 n(恰好是输入数据的行号减11)。
所给的数据都是正确的,不必检验。
//感谢 黄小U饮品 修正输入格式
输出格式
共5行,每行是两个用空格隔开的正整数,依次表示前55名学生的学号和总分。
输入输出样例
输入 #1复制
6 90 67 80 87 66 91 78 89 91 88 99 77 67 89 64 78 89 98
输出 #1复制
6 265 4 264 3 258 2 244 1 237
输入 #2复制
8 80 89 89 88 98 78 90 67 80 87 66 91 78 89 91 88 99 77 67 89 64 78 89 98
输出 #2复制
8 265 2 264 6 264 1 258 5 258
分析:
这题其实并不难理解,就敲代码实现就好了,不过由于我用的是二维数组来分别存储 语文,数学,英语,总分,学号,因此就不能用sort直接对总分进行排序了,那就手写快排吧,还好没忘。注意快排的时候两个值交换的时候,要把每一项都交换
然后就是要注意,总分相等语文优先,语文相等学号优先,这个我实现起来有些麻烦,但只有五个数,所以并不会超时,如果数多了就不一定了...上AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
int n;
int a[305][6];
void quicksort(int left,int right);
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i][1];
cin>>a[i][2];
cin>>a[i][3];
a[i][4]=a[i][1]+a[i][2]+a[i][3];
a[i][5]=i;
}
quicksort(1,n);
// for(int i=1;i<=n;i++)
//cout<<a[i][4]<<" ";
//cout<<endl;
for(int i=n;i>n-5;i--)
{
// cout<<"hello"<<endl;
int temp1=a[i][4];
for(int j=i-1;j>n-5;j--)
{
//cout<<""
if(temp1==a[j][4])
{
int yuwen1=a[i][1];
int yuwen2=a[j][1];
if(yuwen2>yuwen1)
{
//cout<<"yuwen: "<<i<<" "<<j<<endl;
//cout<<a[i][5]<<" "<<a[j][5]<<endl;
int t1=a[i][1];
int t2=a[i][2];
int t3=a[i][3];
int t4=a[i][4];
int t5=a[i][5];
a[i][1]=a[j][1];
a[i][2]=a[j][2];
a[i][3]=a[j][3];
a[i][4]=a[j][4];
a[i][5]=a[j][5];
a[j][1]=t1;
a[j][2]=t2;
a[j][3]=t3;
a[j][4]=t4;
a[j][5]=t5;
}
else if(yuwen1==yuwen2)
{
int xuehao1=a[i][5];
int xuehao2=a[j][5];
if(xuehao2<xuehao1)
{
int t1=a[i][1];
int t2=a[i][2];
int t3=a[i][3];
int t4=a[i][4];
int t5=a[i][5];
a[i][1]=a[j][1];
a[i][2]=a[j][2];
a[i][3]=a[j][3];
a[i][4]=a[j][4];
a[i][5]=a[j][5];
a[j][1]=t1;
a[j][2]=t2;
a[j][3]=t3;
a[j][4]=t4;
a[j][5]=t5;
}
}
}
}
}
for(int i=n;i>n-5;i--)
cout<<a[i][5]<<" "<<a[i][4]<<endl;
return 0;
}
void quicksort(int left,int right)
{
int low=left;
int high=right;
int temp=(low+high)/2;
do
{
while(a[low][4]<a[temp][4]) low++;
while(a[high][4]>a[temp][4]) high--;
if(low<=high)
{
int temp1=a[low][1];
int temp2=a[low][2];
int temp3=a[low][3];
int temp4=a[low][4];
int temp5=a[low][5];
a[low][1]=a[high][1];
a[low][2]=a[high][2];
a[low][3]=a[high][3];
a[low][4]=a[high][4];
a[low][5]=a[high][5];
a[high][1]=temp1;
a[high][2]=temp2;
a[high][3]=temp3;
a[high][4]=temp4;
a[high][5]=temp5;
low++;
high--;
}
if(left<high) quicksort(left,high);
if(right>low) quicksort(low,right);
}while(low<=high);
}