学生数据排序
學生數據排序
Description
某班 有n個學生(不超過100),每個學生的信息包括學號(long no),姓名(char name[9])和三門課的成績(int score[4],其中score[0]~score[3]分別用來存放總分,語文,數學和英語的成績)。
編寫程序,錄入n個學生的信息,然后按照總分降序排序(如果總分相同的按照學號小的在前),最后輸出排序后的學生信息。
注意:main函數已經給定(如下所示)。
? ? ? ? ? ?請將程序補充完整。
? ? ? ? ? ?提交時只需要提交自己補充的代碼部分,不需要提交給定的main函數的代碼部分。
#include<stdio.h>int main()
{int n;struct student stu[100];while(scanf("%d",&n)!=EOF){input(stu,n); //讀入n個學生的數據sort(stu,n); //按照總分降序排序,如果總分相同的學號小的在前面print(stu,n); //輸出n個學生的信息}return 0;
}
Input
?包含多組測試數據,每組測試數據第一行包含1個整數n(大于1小于等與100),表示學生人數。接下來的n行為n個學生的詳細信息,包括學號,姓名,語文,數學和英語的分數,個數據之間用空格隔開。
Output
?每組測試數據輸出占n行,每行輸出一個學生的信息。每一行的輸出數據依次為:排名,學號,姓名,總分,英文成績,數學成績和英語成績。各部分數據之間用空格隔開。(具體詳見樣例輸出)
Sample Input
5 1001 aaa 54 85 65 1002 bbb 54 65 85 1003 ccc 88 99 99 1004 ddd 35 50 50 1005 eee 99 99 99
?
Sample Output
1 1005 eee 297 99 99 99 2 1003 ccc 286 88 99 99 3 1001 aaa 204 54 85 65 4 1002 bbb 204 54 65 85 5 1004 ddd 135 35 50 50
題目分析:
仔細讀題,能讀懂這題的意思,一個結構體的好例題。
C語言中結構體的用法:
參考https://blog.csdn.net/eric_e/article/details/79340947
涉及到結構體的互換,需要另外定義一個結構體來交換。(詳見代碼中的 “ t ” )
我用的冒泡排序(因為不想手打快排,說白了就是懶。。)
打代碼的時候一定要仔細……結構體看得眼花……
以下提供完整代碼,抄作業是不對的喲~
#include<stdio.h>
struct student
{char name[9];long no;int score[4];
};
void input(struct student stu[100],int n)
{int i;for(i=1;i<=n;i++){scanf("%ld %s %d %d %d",&stu[i].no,&stu[i].name,&stu[i].score[1],&stu[i].score[2],&stu[i].score[3]);stu[i].score[0]=stu[i].score[1]+stu[i].score[2]+stu[i].score[3];}return;
}
void sort(struct student stu[100],int n)
{struct student t;int i,j;for(i=1;i<=n;i++)for(j=i+1;j<=n;j++){if(stu[i].score[0]<stu[j].score[0] || stu[i].score[0]==stu[j].score[0] && stu[i].no>stu[j].no){t=stu[i];stu[i]=stu[j];stu[j]=t;}}return;
}
void print(struct student stu[100],int n)
{int i;for(i=1;i<=n;i++){printf("%d ",i);printf("%ld %s %d %d %d %d\n",stu[i].no,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3]);}return;
}
int main()
{int n;struct student stu[100];while(scanf("%d",&n)!=EOF){input(stu,n); //讀入n個學生的數據sort(stu,n); //按照總分降序排序,如果總分相同的學號小的在前面print(stu,n); //輸出n個學生的信息}return 0;
}
?
總結
- 上一篇: 君非良人下一句是什么呢?
- 下一篇: 撒尔莫路亚竿怎么样多少钱一支?