Problem
Description
某校举办活动。先从每班学生中选出前三名进入复试,再从进入复试的所有人中选出前三名评为校xx活动之星。
Input
第一行一个正整数N,表示班级数量,随后有N行输入,每行第一个数字为该班人数K,后续有K个数据,代表该班K个学生的成绩。
Output
三位校xx活动之星的成绩在各自班级总成绩中的比重。
Sample
Input
2
5 99 95 38 45 34
3 79.5 34.4 25.7
Output
三个数字,第一第二个数字、第二第三数字之间各有一个空格。
Code
先找到一个班级的成绩前三名,当作三位校xx活动之星,然后其他班级,若有分数更高的学生,则替换相应的校xx活动之星,该学生成为新的校xx活动之星。
#include <stdio.h>
struct stu {
float score;//一个学生的分数
float total;//该学生所在班级的总分数
int flag;//令flag默认值为0,若score更改过,班级就可能发生变化,则flag变为1
};
int main()
{
int N;//班级数量
int K;//班级人数
float s;
struct stu stu[3] = { //记录三个校xx活动之星
{0,0,0} ,
{0,0,0} ,
{0,0,0} };
float sum = 0;//某个班级的总分数
scanf("%d", &N);
while (N--)//找到三个校xx活动之星
{
scanf("%d", &K);
for (int i = 0; i < K; i++)
{
scanf("%f", &s);
sum += s;//累加,得到某个班级的总分数
if (s > stu[0].score) {
stu[0].score = s;
stu[0].flag = 1;//score发生变化,flag置1
}
else if (s > stu[1].score) {
stu[1].score = s;
stu[1].flag = 1;
}
else if(s > stu[2].score){
stu[2].score = s;
stu[2].flag = 1;
}
}//for
for (int i = 0; i < 3; i++) {
if (stu[i].flag == 1)
stu[i].total = sum;
stu[i].flag = 0;//重置flag
}
sum = 0;//清零,求下一个班级的总分数
}//while
float ss[3];
for (int i = 0; i < 3; i++) {//计算成绩比重
ss[i] = stu[i].score / stu[i].total;
}
printf("%f %f %f\n", ss[0], ss[1], ss[2]);
return 0;
}
若有错误请留言。