码迷,mamicode.com
首页 > 其他好文 > 详细

成绩排名

时间:2019-01-06 14:32:37      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:can   语文   typedef   min   类型   文件   col   为什么   %s   

  

题目描述

每次期末考试成绩出来之前的一段时间大豪哥心里都是痛苦的,总感觉自己会在班上排名特别差。所以当成绩出来以后大豪哥想快点知道班上的总排名,以便知道自己的排名。(PS:大豪哥班上有个学霸名叫日天,又名泰迪,不要问我为什么,因为泰迪的行为决定的)

 

输入

多组测试数据,至文件结尾。
先输入每个班上有n个同学,这个学期有m门课程(1<=n,m<=100)
接下来有n行,每行的输入格式为学号id,姓名name,课程成绩k1,课程成绩k2... ,课程成绩km.(学号在int型范围以内,0<=ki<=100,姓名长度在20个字符以下)

 

输出

输出班上每个同学排名,并输出总分及排名(如果总分相同就按学号从小到大输出,但是排名还是相同的)输出格式见样例

 

样例输入

4 4
1 Taidi 100 100 90 90
2 Dahaoge 60 60 70 60
3 Yeshen 90 90 100 100
4 Wangpangzi 50 60 100 80

样例输出

1 Taidi 100 100 90 90 Sum = 380 Ranking = 1
3 Yeshen 90 90 100 100 Sum = 380 Ranking = 1
4 Wangpangzi 50 60 100 80 Sum = 290 Ranking = 3
2 Dahaoge 60 60 70 60 Sum = 250 Ranking = 4

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 
  5 //创建STUDENT类型变量 包括学号、姓名、数学, 英语, 语文的成绩
  6 typedef struct student {
  7     int id;
  8     char name[50];
  9     int score[100];
 10 
 11     struct  student *pNext;
 12 }STUDENT;
 13 //创建链表
 14 STUDENT *Create(int n,int m) {
 15     STUDENT *pHead, *pEnd, *pNew = NULL;
 16     int i,j;
 17     pHead = pEnd = (STUDENT*)malloc(sizeof(STUDENT));
 18     for (i = 0; i < n; i++)
 19     {
 20         pNew = (STUDENT*)malloc(sizeof(STUDENT));
 21         scanf("%d%s", &pNew->id, pNew->name);
 22         for(j=0;j<m;j++)
 23         {
 24             scanf("%d", &pNew->score[j]);
 25         }
 26 
 27         pEnd->pNext = pNew;
 28         pEnd = pNew;
 29 
 30     }
 31     return pHead;
 32 }
 33 
 34 //寻找前驱节点
 35 STUDENT *Find_before(STUDENT* phead, STUDENT* p)
 36 {
 37     if (!p) return NULL;
 38     STUDENT *pbefore = phead;
 39     while (pbefore)
 40     {
 41         if (pbefore->pNext == p)
 42             return pbefore;
 43         pbefore = pbefore->pNext;
 44     }
 45     return NULL;
 46 }
 47 int sumscore(STUDENT *p, int m) {
 48     int i,sum=0;
 49 
 50     for(i=0;i<m;i++)
 51     {
 52 
 53         sum += p->score[i];
 54     }
 55     return sum;
 56 }
 57 
 58     void Sort(STUDENT *head, int n, int m) {
 59     STUDENT *pHead, *pEnd, *q = NULL, *qbefore = NULL, *p = NULL;
 60     int maxsumscore, minid;
 61     pHead = pEnd = (STUDENT*)malloc(sizeof(STUDENT));
 62     while (head->pNext != NULL)
 63     {
 64         maxsumscore = sumscore(head->pNext, m);
 65         minid = head->pNext->id;
 66         q = p = head->pNext;
 67         //寻找最高总分且最小学号的节点
 68         while (p->pNext != NULL)
 69         {
 70             if (maxsumscore < sumscore(p->pNext,m))
 71             {
 72                 maxsumscore = sumscore(p->pNext, m); minid = p->pNext->id;  q = p->pNext;
 73             }
 74             if ((maxsumscore == sumscore(p->pNext,m) ) && (minid > p->pNext->id))
 75             {
 76                 maxsumscore = sumscore(p->pNext, m); minid = p->pNext->id; q = p->pNext;
 77             }
 78             p = p->pNext;
 79         }
 80         qbefore = Find_before(head, q);  //寻找q节点的前驱节点
 81         qbefore->pNext = q->pNext; //将q的前驱节点指向q的后驱节点 从而将q节点从a链表中剔除
 82 
 83         pEnd->pNext = q;    //将头指针指向q
 84         q->pNext = NULL;    //q节点指向空
 85         pEnd = q;            //更新尾节点
 86 
 87     }
 88     free(head);//释放head链表头节点
 89     STUDENT *t; t = pHead;
 90     int sum[100],i,j,ranking[100];
 91     for (i = 0; i < n; i++)
 92     {
 93         t = t->pNext;
 94         sum[i] = sumscore(t, m);
 95     }
 96     ranking[0] = 1;
 97     for (i = 0; i < n; i++)
 98     {
 99         j = i + 1;
100         if (sum[i] == sum[j]) ranking[j] = 1;
101         else  ranking[j] = j + 1;
102 
103    }
104     t = pHead->pNext;
105     for (i = 0; i < n; i++)
106     {
107         printf("%d %s ", t->id, t->name);
108         for (j = 0; j < m; j++)
109         {
110             printf("%d ", t->score[j]);
111         }
112         printf("Sum = %d Ranking = %d\n", sum[i],ranking[i]);
113         t = t->pNext;
114     }
115     free(pHead);
116 }
117 
118 
119 
120 int main() {
121     int n, m;
122      STUDENT *p;
123      scanf("%d%d", &n, &m);
124      p = Create(n,m);
125      Sort(p, n, m);
126      return 0;
127 }

 

 

成绩排名

标签:can   语文   typedef   min   类型   文件   col   为什么   %s   

原文地址:https://www.cnblogs.com/mwq1024/p/10228717.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!