本蒟蒻在线急等

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

platelet2021 @ 2022-08-06 09:10:04

本蒟蒻在线急等

为什么第一个点RE了~

#include<bits/stdc++.h>
using namespace std;
struct student {
    string s;
    int yw, sx, yy, id, zf;

} a[1001];
int best(int a, int b[]) {
    int flag = b[1];
    int ID = 1;
    for (int i = 1; i <= a; i++) {
        if (flag < b[i]) {
            flag = b[i];
            ID = i;
        }

    }
    return ID;
}
int main() {
    //with good luck
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].s >> a[i].yw >> a[i].sx >> a[i].yy;
        a[i].zf = a[i].yw + a[i].sx + a[i].yy;
        a[i].id = i;
    }
    int flag, ID;
    for (int i = 1; i <= n; i++) {
        if (flag < a[i].zf) {
            flag = a[i].zf;
            ID = i;
        }
    }
    cout << a[ID].s << " " << a[ID].yw << " " << a[ID].sx << " " << a[ID].yy;
    return 0;
}

by hjsxhst2022 @ 2022-08-06 09:21:13

@platelet2021 应该是哪里越界访问数组了,是内存引用无效的分段故障


by platelet2021 @ 2022-08-06 09:23:25

@hjsxhst2022 不懂,啥叫内存引用无效的分段故障 谢谢


by yinqf @ 2022-08-06 09:26:43

int flag, ID; flag没初始值

改成 int flag=-1, ID;


by yinqf @ 2022-08-06 09:27:39

改完就AC了


by platelet2021 @ 2022-08-06 09:28:05

@yinlg 懂了,谢谢


by yinqf @ 2022-08-06 09:28:33

不客气


by hjsxhst2022 @ 2022-08-06 09:30:00

@platelet2021 就是一个地址是非法的,它可以指向任何内存位置,计算机不知道要用哪个内存位置,所以就是内存引用无效。

本人并不保证以上内容是否准确


by hjsxhst2022 @ 2022-08-06 09:34:22

@platelet2021 我第一个点没RE,但是它WA了

错误提示:Wrong Answer.wrong answer On line 1 column 1, read (ASCII 32), expected p.


by platelet2021 @ 2022-08-06 10:41:26

@hjsxhst2022 第一个点应该是和临界值有关的,我也不太确定


|