c++ 在主函数中创建一个有5个元素的结构数组,输出其中成绩最高的学生的姓名

定义一个结构体学生,有三个成员:学号,姓名name和成绩score,定义如下:
struct STUDENT
{ int no; //学号
string name; //姓名
int score; //成绩
}; 在主函数中创建一个有5个元素的结构数组,输出其中成绩最高的学生的姓名

又来一遍?

#include <iostream>
using namespace std;
#include <string>
 
typedef struct _STUDENT
{ int no; //学号
string name; //姓名
int score; //成绩
}STUDENT;
int main()
{
    STUDENT s[5];
    int max = 0;
    for(int i=0;i<5;i++)
    {
        cin>>s[i].no>>s[i].name>>s[i].score;
        if(s[i].score> s[max].score)
            max = i;
    }
    cout<<"成绩最高的是:"<<s[max].score;
}


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

struct STUDENT {
    int no; //学号
    string name; //姓名
    int score; //成绩
};

int main() {
    STUDENT students[5] = { //创建一个有5个元素的结构数组
        {1, "Tom", 89},
        {2, "Jerry", 92},
        {3, "Alice", 77},
        {4, "Bob", 85},
        {5, "Lucy", 96}
    };

    int maxScore = -1; //最高成绩
    string maxName = ""; //最高成绩的学生姓名

    for (int i = 0; i < 5; i++) {
        if (students[i].score > maxScore) { //如果当前学生的成绩比最高成绩高
            maxScore = students[i].score; //更新最高成绩
            maxName = students[i].name; //更新最高成绩的学生姓名
        }
    }

    cout << "最高成绩的学生是:" << maxName << endl;

    return 0;
}

输出:

img

供参考:

#include <iostream>
#include <string>
using  namespace std;
struct STUDENT{
    int    no; //学号
    string name; //姓名
    int score; //成绩
};
int main()
{
    int i, max;
    struct STUDENT stu[5];
    for (i = 0, max = 0; i < 5; i++) {
        cin >> stu[i].no >> stu[i].name >> stu[i].score;
        if (stu[i].score > stu[max].score) max = i;
    }
    cout << stu[max].no << " " << stu[max].name << " " << stu[max].score;
    return 0;
}