题干解读:输入1个数N,接下来N行输入学生的信息,要求将输入的学生的信息进行比较,选出平均分最高的学生,并输出他的信息
解体思路:使用学生类来记录一个学生的不同信息,最后通过比较选出平均分最高的学生,输出其信息即可。
#include <iostream>
#include<string>
using namespace std;
class Student{
private:
string s;
int c1,c2,c3;
public:
double T;
Student(){
}
Student(string s,int c1,int c2,int c3){
this->s = s;
this->c1 =c1;
this->c2 =c2;
this->c3 = c3;
}
void Creat(string s,int c1,int c2,int c3){
this->s = s;
this->c1 =c1;
this->c2 =c2;
this->c3 = c3;
}
void Sum(){
T = (c1+c2+c3)/3.0;//注意点:这里如果使用整除会出错.
}
void Print(){
cout<<s<<" "<<c1<<" "<<c2<<" "<<c3;
}
};
int main() {
int n;
cin>>n;
Student student[n];
string s;
int c1,c2,c3;
for(int i=0;i<n;i++){
cin>>s>>c1>>c2>>c3;
student[i].Creat(s,c1,c2,c3);
student[i].Sum();
}
int max = student[0].T;
int flag = 0;
for(int i=1;i<n;i++){
if(student[i].T>max){
max = student[i].T;
flag = i;
}
}
student[flag].Print();
}

京公网安备 11010502036488号