C语言 谁总分第一(测试你对结构体数组的熟练程度)

题目描述

有N个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入N个学生的数据,要求找出总分第一且没有单科不及格的学生的数据(包括学号、姓名、3门课成绩)并输出。

结构体类型及结构体数组的定义可参考如下定义。题目没有明确说明的,均没有严格限制。

#define N 100
struct student
{
  char num[10];
  char name[10];
  int score[4];
  int total;
}stu[N];

输入

先输入学生数量N
然后每行输入一个学生的数据,包括学号、姓名、三科成绩,空格分开。

输出

总分第一且没有单科不及格的学生的数据(包括学号、姓名、3门课成绩),如果存在并列总分第一的,只输出从前往后查找遇到的第一个学生的数据。

样例输入 Copy

4
1101 clan 80 70 60
1102 blue 90 80 70
1103 xds 59 99 98
1104 bred 80 80 80

样例输出 Copy

1102 blue 90 80 70

代码

#include<stdio.h>
#include<math.h>
#include<string.h> 
#include<stdlib.h>
#define N 100

struct student
{
	int num;
	char name[10];
	int score[4];
	int total;
}stu[N];

int main()
{
	int n;
	scanf("%d",&n);
	int i,max,k=0;
	for(i=0;i<n;i++)
	{
		scanf("%d %s %d %d %d",&stu[i].num,&stu[i].name ,&stu[i].score[0] ,&stu[i].score[1],&stu[i].score[2]);
		stu[i].total=stu[i].score[0]+stu[i].score[1]+stu[i].score[2];
		
		if(i==0)max=stu[i].total;
		else
		{
				if(stu[i].total>max&&stu[i].score[2]>=60&&stu[i].score[1]>=60&&stu[i].score[0]>=60)
				{
					max=stu[i].total;
					k=i;
				}
		}
	}
	printf("%d %s %d %d %d",stu[k].num,stu[k].name ,stu[k].score[0] ,stu[k].score[1],stu[k].score[2]);
}
发布了47 篇原创文章 · 获赞 29 · 访问量 1491

猜你喜欢

转载自blog.csdn.net/Qianzshuo/article/details/103758352