有10个学生,每个学生的数据包括学号、姓名、3门课程的成绩,从键盘输入10个学生数据 要求输出3门课程总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课程成绩)

#include <iostream>
#include <string>
using namespace std;

struct Student {
	string num;
	string name;
	float score[3];
};

int main() 
{
	Student students[10];
	float sum = 0.0, max = 0.0;
	int maxIndex = 0;

	cout << "请输入学生信息:" << endl;
	for (int i = 0; i < 10; i++) 
	{
		cout << "学生学号:";
		cin >> students[i].num;
		cout << "学生姓名:";
		cin >> students[i].name;
		cout << "学生成绩:";
		cin >> students[i].score[0] >> students[i].score[1] >> students[i].score[2];
		cout << endl;
	}

	for (int i = 0; i < 10; i++) 
	{
		sum = 0.0;//易忽略
		for (int j = 0; j < 3; j++) 
		{
			sum += students[i].score[j];
		}
		cout <<"学号:"<<students[i].num<< "的平均成绩是:" << sum / 3 << endl;
		cout << endl;
		//判断是否最高
		if (sum / 3 > max) 
		{
			max = sum / 3;
			maxIndex = i;
		}
	}

	cout << endl;
	cout << "最高分的学生学号是:" << students[maxIndex].num << endl;
	cout << "姓名是:" << students[maxIndex].name << endl;
	cout << "成绩是:" << students[maxIndex].score[0] << " " << students[maxIndex].score[1] << " " << students[maxIndex].score[2] << endl;
	cout << "平均成绩是:" << max << endl;

	return 0;
}
#include <iostream>
#include <string>
using namespace std;

struct Student {
	string num;
	string name;
	float* score;
};

int main()
{
	Student* students = new Student[10];
	for (int i = 0; i < 10; i++) 
	{
		cout << "学生学号:";
		cin >> students[i].num;
		cout << "学生姓名:";
		cin >> students[i].name;

		students[i].score = new float[3];
		cout << "学生成绩:";
		cin >> students[i].score[0] >> students[i].score[1] >> students[i].score[2];
		cout << endl;
	}

	float maxAvg = 0.0;
	int maxIndex = 0;

	for (int i = 0; i < 10; i++) 
	{
		float sum = 0.0;
		for (int j = 0; j < 3; j++)
		{
			sum += students[i].score[j];
		}
		float avg = sum / 3;
		cout << "平均成绩是:" << avg << endl;

		if (avg > maxAvg)
		{
			maxAvg = avg;
			maxIndex = i;
		}
	}
	cout << endl;
	cout << "最高分的学生学号是:" << students[maxIndex].num << endl;
	cout << "姓名是:" << students[maxIndex].name << endl;
	cout << "成绩是:" << students[maxIndex].score[0] << " " << students[maxIndex].score[1] << " " << students[maxIndex].score[2] << endl;
	cout << "平均成绩是:" << maxAvg << endl;
	for (int i = 0; i < 10; i++) 
	{
		delete[] students[i].score;
	}
	delete[] students;

	return 0;
}

  • 10
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值