PTA——7-4 找出总分最高的学生 (15 分)

给定N个学生的基本信息,包括学号(由5个数字组成的字符串)、姓名(长度小于10的不包含空白字符的非空字符串)和3门课程的成绩([0,100]区间内的整数),要求输出总分最高学生的姓名、学号和总分。

输入格式:

输入在一行中给出正整数N(≤10)。随后N行,每行给出一位学生的信息,格式为“学号 姓名 成绩1 成绩2 成绩3”,中间以空格分隔。题目保证这样的学生是唯一的。

输入样例:

5
00001 huanglan 78 83 75
00002 wanghai 76 80 77
00003 shenqiang 87 83 76
10001 zhangfeng 92 88 78
21987 zhangmeng 80 82 75

结尾无空行

输出样例:

zhangfeng 10001 258

结尾无空行

看见网上的解答很多是用结构做的,但是我第一次就是用面向对象做的,仅供参考。本人仅初学c++,细节处理不到位还请指正。
 

#include<iostream>
#include<string>
using namespace std;
class student
{
public:
	student() {};//无参构造函数
	void Print(student stu[], int t);
	void set(student stu[], int n);
	int FindMax(student stu[],int n);
private:
	string _num;
	string _name;
	int _score1, _score2, _score3;
	int _sum = _score1 + _score1 + _score3;
};
void student::Print(student stu[], int t)//输出打印函数
{
	cout << stu[t]._name << " " << stu[t]._num << " " << stu[t]._sum << endl;
}
void student::set(student stu[],int n)//输入函数
{
	string num;
	string name;
	int score1;
	int score2;
	int score3;
	int sum;
	for (int i = 0; i < n; i++)
	{
		cin >> num >> name >> score1 >> score2 >> score3;
		stu[i]._num = num;
		stu[i]._name = name;
		stu[i]._score1 = score1;
		stu[i]._score3 = score3;
		stu[i]._score3 = score3;
		stu[i]._sum = score1 + score2 + score3;
	}
}
int student::FindMax(student stu[],int n)//找出最大值函数 
{
	int max = stu[0]._sum;
	int t = 0;
	for (int i = 0; i < n; i++)
	{
		if (stu[i]._sum > max)
		{
			max = stu[i]._sum;
			t = i;
		}
	}
	return t;
}
int main()
{
	int n;
	cin >> n;
	student stu[10],a;//对象数组与对象
	a.set(stu, n);
	a.Print(stu,a.FindMax(stu,n));
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值