解题思路:
题目讲述的非常清楚,输入几个人和他们的成绩与学号,在按照他们的总分,语文成绩或者学号进行排名,所以应运用结构体,在编写几个子函数就可简便的做出题解,以下见代码参考
注意事项:
算总分的子函数要用一级指针。
total预先设为0否则后面运算的结果可能出错。
参考代码:
#include <iostream>
int n;
typedef struct student{
int score[3];
int total=0;
int m;
}student;
void calTotal(student* ps){
for(int i=0;i<3;i++)
ps->total += ps->score[i];
}
void exchange(student* x, student* y){
student temp = *x;
*x = *y;
*y = temp;
}
void sort(student s[]){
for(int j=n-1;j>0;j--){
for(int i=0;i<j;i++){
if(s[i].total<s[i+1].total)
exchange(&s[i], &s[i+1]);
if(s[i].total==s[i+1].total){
if(s[i].score[0]<s[i+1].score[0])
exchange(&s[i], &s[i+1]);
if(s[i].score[0]==s[i+1].score[0])
if(s[i].m>s[i+1].m)
exchange(&s[i], &s[i+1]);
}
}
}
}
int main(){
scanf("%d",&n);
student s[n];
for(int i=0;i<n;i++){
scanf("%d %d %d",&s[i].score[0],&s[i].score[1],&s[i].score[2]);
s[i].m=i+1;
}
for(int i=0;i<n;i++)
calTotal(s+i);
sort(s);
for(int i=0;i<5;i++)
printf("%d %d\n",s[i].m,s[i].total);
return 0;
}
0.0分
2 人评分
C语言程序设计教程(第三版)课后习题8.5 (C语言代码)浏览:935 |
【绝对值排序】 (C语言代码)浏览:820 |
C语言程序设计教程(第三版)课后习题8.7 (C语言代码)浏览:915 |
杨辉三角 (C语言代码)浏览:484 |
1052题解(链表操作)浏览:651 |
C语言程序设计教程(第三版)课后习题8.2 (C语言代码)浏览:1028 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:449 |
整数分类问题 为什么输出总是数字8啊浏览:442 |
IP判断 (C++代码)浏览:746 |
C语言程序设计教程(第三版)课后习题6.2 (C语言代码)浏览:566 |