就在刚刚,我终于解决了一道困惑我许久的问题,并从中学到了很多新东西,答案正确的那一瞬间真的太激动了,先看题目(我还是个菜鸟,不要嘲讽我题目简单呜呜)
输入n个学生数据记录(姓名、学号,5门课成绩)算出总分并且进行排序,按总分最后从高到低输出这些数据。
输入
第一行输入学生的个数N接下来的N行输入学生的姓名,学号,5门科的成绩。
输出
一共输出N行,每行输出学生的姓名,学号,及总分(格式:total=?),每个数据相隔一个空格
输入样例 1
3 A 200701 98 125 100 96 85 B 200702 100 100 100 100 120 C 200703 130 80 70 50 50
输出样例 1
B 200702 total=520 A 200701 total=504 C 200703 total=380
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
#include<string.h>
#include<math.h>
//
//输入n个学生数据记录(姓名、学号,5门课成绩)算出总分并且进行排序,按总分最后从高到低输出这些数据。
//
//
//输入
//第一行输入学生的个数N接下来的N行输入学生的姓名,学号,5门科的成绩。
//
//
//输出
//一共输出N行,每行输出学生的姓名,学号,及总分(格式:total=?),每个数据相隔一个空格
//
//
//输入样例 1
//
//3
//A 200701 98 125 100 96 85
//B 200702 100 100 100 100 120
//C 200703 130 80 70 50 50
//输出样例 1
//
//B 200702 total=520
//A 200701 total=504
//C 200703 total=380
//
int main()
{
int i,j,x;
struct Student{
char name[100];
int degree;
int score[5];
int sum;
};
int n;
scanf("%d",&n);
struct Student student[n];
for(i=0;i<n;i++)
{
student[i].sum=0;
}
for(i=0;i<n;i++)
{
scanf("%s",&student[i].name);
scanf("%d",&student[i].degree);
for(x=0;x<5;x++)
{
scanf("%d",&student[i].score[x]);
student[i].sum+=student[i].score[x];
}
}
//把sum按从高到低排序
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(student[j+1].sum>student[j].sum)
{
struct Student m;
m=student[j+1];
student[j+1]=student[j];
student[j]=m;
}
}
}
for(i=0;i<n;i++){
printf("%s %d total=%d\n",student[i].name,student[i].degree,student[i].sum);
}
return 0;
}
为了这道题目学会了结构体,冒泡排序等知识,真的让我收获很多,浅浅记录一下