c++20分求助!!希望大佬帮帮我

P5740 【深基7.例9】最厉害的学生

Aloteri @ 2023-03-21 21:13:20

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

struct Student
{   
    string m_Name;
    int c, m, e ,score=c+m+e;

}stu[1000];
int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> stu[i].m_Name >> stu[i].c >> stu[i].m >> stu[i].e;
    }

    int max = stu[0].score, maxIndex = 0;
    for (int i = 0; i < n; i++)
    {
        if (stu[i].score > max) {
            maxIndex = i;
        }
    }
    cout << stu[maxIndex].m_Name <<" " << stu[maxIndex].c <<" " << stu[maxIndex].m <<" "<< stu[maxIndex].e << endl;
    return 0;
}

by silent_ST @ 2023-03-21 21:23:52

for (int i = 0; i < n; i++)
    {
        if (stu[i].score > max) {
            max = stu.[i].score;
            maxIndex = i;
        }
    }

要记得更新max


by Aloteri @ 2023-03-21 21:26:26

@bella2010 感谢,但是我刚提交依然20分,还是有问题@_@


by Geirangerfjard @ 2023-03-21 21:27:46

@Aloteri accode

#include <iostream>

using namespace std;

const int N = 1001;

int n;

int mxtot, pos;

struct stu
{
    int c, m, e, tot;
    string name;
}s[N];

int main()

{
    mxtot = -1e5;
    cin >> n;
    for (int i = 1; i <= n; i ++ )
    {
        cin >> s[i].name >> s[i].c >> s[i].m >> s[i].e;
        s[i].tot = s[i].c + s[i].m + s[i].e;
        if (s[i].tot > mxtot)
        {
            mxtot = s[i].tot;
            pos = i;
        }
    }

    cout << s[pos].name << " " << s[pos].c << " " << s[pos].m <<" " << s[pos].e << endl;
}

by Geirangerfjard @ 2023-03-21 21:31:23

@Aloteri 更新max之后改一下stu的范围,因为有可能有极端数据,建议比范围多开一点


by Geirangerfjard @ 2023-03-21 21:31:51

@Alone_Helpless 接着建议score在输入时更新,不然可能有问题


by Geirangerfjard @ 2023-03-21 21:32:23

@Aloteri 改一下就过了

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

struct Student
{   
    string m_Name;
    int c, m, e ,score;

}stu[1010];
int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> stu[i].m_Name >> stu[i].c >> stu[i].m >> stu[i].e;
        stu[i].score = stu[i].c + stu[i].m + stu[i].e;
    }

    int max = stu[0].score, maxIndex = 0;
    for (int i = 0; i < n; i++)
    {
        if (stu[i].score > max) {
            maxIndex = i;
            max = stu[i].score;
        }
    }
    cout << stu[maxIndex].m_Name << " " << stu[maxIndex].c << " " << stu[maxIndex].m <<" "<< stu[maxIndex].e << endl;
    return 0;
}

by Geirangerfjard @ 2023-03-21 21:34:01

@Aloteri 看一下有问题吗


by HY248 @ 2023-03-21 21:34:18

您的代码存在以下问题:

  1. 在定义结构体时
    int c, m, e ,score=c+m+e;
for (int i = 0; i < n; i++)
    {
        stu[i].score = stu[i].c + stu[i].m + stu[i].e;
    }
  1. 在打擂台找最大值的时候 max 的值没更新,也可以写成以下形式:
        if (stu[i].score > stu[maxIndex].score) {
            maxIndex = i;
        }

完整代码:

#include<iostream>
#include<string>
using namespace std;
struct Student
{   
    string m_Name;
    int c, m, e ,score;

}stu[1000];
int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> stu[i].m_Name >> stu[i].c >> stu[i].m >> stu[i].e;
    }
    for (int i = 0; i < n; i++)
    {
        stu[i].score = stu[i].c + stu[i].m + stu[i].e;
    }
    int max = stu[0].score, maxIndex = 0;
    for (int i = 1; i < n; i++)
    {
        if (stu[i].score > stu[maxIndex].score) {
            maxIndex = i;
        }
    }
    cout << stu[maxIndex].m_Name <<" " << stu[maxIndex].c <<" " << stu[maxIndex].m <<" "<< stu[maxIndex].e << endl;
    return 0;
}

最后,加油!


by HY248 @ 2023-03-21 21:36:14

回复好快

%%%%


by Aloteri @ 2023-03-21 21:36:48

@Alone_Helpless 跪谢跪谢!!!!


| 下一页