零K沁雪


私信TA

用户名:qczl

访问量:74752

签 名:

零K沁雪

等  级
排  名 40
经  验 12031
参赛次数 3
文章发表 35
年  龄 0
在职情况 在职
学  校
专  业

  自我简介:

解题思路:

1、使用结构体存放学生信息

2、成员 index ,用来区分每个学生

3、成员 score[4] 存放各科成绩和总成绩

4、输入人数不定,所以结构体大小由 malloc 动态分配

注意事项:

参考代码:

#include <stdio.h>
#include <malloc.h>

typedef struct _stu_info {
	int index;
	/*
	 * score[0] - score[2] 存储各科成绩
	 * score[3] 存储总成绩
	 * */
	int score[4];
	char name[32];
	char stuid[8];
}stuInfo;

/*
 * 获取最高分对应的下标
 * */
int get_tallest_score_index(stuInfo *p, int size)
{
	int i = 0, index = 0;
	for(i = 1; i < size; i++)
	{
		if( p[index].score[3] < p[i].score[3] )
		{
			index = i;
		}
	}
	return index;
}

int main(int argc, char **argv)
{
	int n = 0, sum[3] = {0}, i = 0, tallest = 0;
	scanf("%d", &n);
	if(n < 1)
	{
		return -1;
	}
	stuInfo *info = NULL;
	info = (stuInfo *)malloc(sizeof(stuInfo));
	if(info == NULL)
	{
		return -1;
	}
	for(i = 0; i < n; i++)
	{
		info[i].index = i;
		scanf("%s %s %d %d %d",
				info[i].stuid,
				info[i].name,
				&info[i].score[0],
				&info[i].score[1],
				&info[i].score[2]
				);
		/*计算个人总成绩*/
		info[i].score[3] = info[i].score[0] + info[i].score[1] + info[i].score[2];
		/*以下计算各科总分*/
		sum[0] = sum[0] + info[i].score[0];
		sum[1] = sum[1] + info[i].score[1];
		sum[2] = sum[2] + info[i].score[2];
	}
	/*输出各科平均分*/
	printf("%d %d %d\n", sum[0]/n, sum[1]/n, sum[2]/n);
	/*获取最高分下表*/
	tallest = get_tallest_score_index(info, n);
	/*输出最高分学生成绩*/
	printf("%s %s %d %d %d\n", 
			info[tallest].stuid,
			info[tallest].name,
			info[tallest].score[0],
			info[tallest].score[1],
			info[tallest].score[2]
			);
	return 0;
}


 

0.0分

7 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区