洛谷P1093--奖学金

妖狐艹你老母 2022-10-31 12:19 427阅读 0赞

P1093 [NOIP2007 普及组] 奖学金


原题题意


思路

此题分三次排序,第一次总分排序,如果总分一样,那么语文分数高者靠前,如果语文分数相同,那么看学号,学号小的名词靠前。
我的解法:设置了一个Stu的结构体,将一名同学的总分,学号和语文分数放在其中,再利用qsort对总分进行排序,然后对成绩相同者做相关处理即可。


(数组)交换函数:

  1. void swap(int *a,int *b){
  2. int tmp = *a;
  3. *a = *b;
  4. *b = tmp;
  5. }

qsort中cmp函数对结构体排序的写法:(其中的Stu是结构体的名字)

  1. int cmp(const void *a,const void *b){
  2. return (*(Stu*)a).score < (*(Stu *)b).score? 1:-1;
  3. }

AC代码:

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. #define ll long long
  6. #define MAX_INT 2147483647
  7. using namespace std;
  8. int flag = 0;
  9. int n;
  10. typedef struct{
  11. int score;
  12. int no;
  13. int chinese;
  14. }Stu;
  15. void swap(int *a,int *b){
  16. int tmp = *a;
  17. *a = *b;
  18. *b = tmp;
  19. }
  20. int cmp(const void *a,const void *b){
  21. return (*(Stu*)a).score < (*(Stu *)b).score? 1:-1;
  22. }
  23. int main(){
  24. scanf("%d",&n);
  25. int chinese[n];
  26. Stu stu[n];
  27. for(int i = 0; i < n; i++){
  28. int a,b,c;
  29. scanf("%d %d %d",&a,&b,&c);
  30. stu[i].chinese = a;
  31. int sum = a+b+c;
  32. stu[i].score = sum;
  33. stu[i].no = i;
  34. }
  35. qsort(stu,n,sizeof(stu[0]),cmp);
  36. if(n > 5){
  37. n = 5;
  38. }
  39. for(int i = 0 ;i < n; i++){
  40. if( i < n-1 && stu[i].score == stu[i+1].score){
  41. if(stu[i].chinese < stu[i+1].chinese){
  42. //printf("1\n");
  43. swap(stu[i].no,stu[i+1].no);
  44. }else if(stu[i].chinese == stu[i+1].chinese){
  45. //printf("!2\n");
  46. if(stu[i].no > stu[i+1].no ){
  47. //printf("2\n");
  48. swap(stu[i].no,stu[i+1].no);
  49. }
  50. }else{
  51. ;
  52. }
  53. }
  54. printf("%d %d\n",stu[i].no+1,stu[i].score);
  55. }
  56. return 0;
  57. }

发表评论

表情:
评论列表 (有 0 条评论,427人围观)

还没有评论,来说两句吧...

相关阅读

    相关 P1093 奖学金

    题目描述 某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金。期末,每个学生都有3门课的成绩:语文、数学、英语。先按总分从高到低排序,如果两个同

    相关 p1164

    > P1164 小A点菜 > > 题目描述 > > uim口袋里有剩M元(M<=10000)。 > > 餐馆虽低端,但是菜品种类不少,有N种(N<=100),第i

    相关 P3382

    题目:[点击打开链接][Link 1] 题意:如题,给出一个N次函数,保证在范围\[l,r\]内存在一点x,使得\[l,x\]上单调增,\[x,r\]上单调减。试求出x

    相关 P1540——机器翻译

    题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章。 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义

    相关 P1113 杂务

    洛谷 P1113 杂务 Description `John`的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它。比如:他们要将奶牛集合起

    相关 P1396 营救

                        [洛谷 P1396 营救][P1396]   题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查