洛谷P1093--奖学金
P1093 [NOIP2007 普及组] 奖学金
原题题意
思路
此题分三次排序,第一次总分排序,如果总分一样,那么语文分数高者靠前,如果语文分数相同,那么看学号,学号小的名词靠前。
我的解法:设置了一个Stu的结构体,将一名同学的总分,学号和语文分数放在其中,再利用qsort对总分进行排序,然后对成绩相同者做相关处理即可。
(数组)交换函数:
void swap(int *a,int *b){
int tmp = *a;
*a = *b;
*b = tmp;
}
qsort中cmp函数对结构体排序的写法:(其中的Stu是结构体的名字)
int cmp(const void *a,const void *b){
return (*(Stu*)a).score < (*(Stu *)b).score? 1:-1;
}
AC代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
#define MAX_INT 2147483647
using namespace std;
int flag = 0;
int n;
typedef struct{
int score;
int no;
int chinese;
}Stu;
void swap(int *a,int *b){
int tmp = *a;
*a = *b;
*b = tmp;
}
int cmp(const void *a,const void *b){
return (*(Stu*)a).score < (*(Stu *)b).score? 1:-1;
}
int main(){
scanf("%d",&n);
int chinese[n];
Stu stu[n];
for(int i = 0; i < n; i++){
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
stu[i].chinese = a;
int sum = a+b+c;
stu[i].score = sum;
stu[i].no = i;
}
qsort(stu,n,sizeof(stu[0]),cmp);
if(n > 5){
n = 5;
}
for(int i = 0 ;i < n; i++){
if( i < n-1 && stu[i].score == stu[i+1].score){
if(stu[i].chinese < stu[i+1].chinese){
//printf("1\n");
swap(stu[i].no,stu[i+1].no);
}else if(stu[i].chinese == stu[i+1].chinese){
//printf("!2\n");
if(stu[i].no > stu[i+1].no ){
//printf("2\n");
swap(stu[i].no,stu[i+1].no);
}
}else{
;
}
}
printf("%d %d\n",stu[i].no+1,stu[i].score);
}
return 0;
}
还没有评论,来说两句吧...