大佬们来看看,本地都是对的,为什么叫上去全RE了??

P1093 [NOIP2007 普及组] 奖学金

hierophant_green @ 2019-11-16 10:27:39

#include<bits/stdc++.h>
using namespace std;
struct node{
    int ch;
    int en;
    int ma;
    int id;
    int tot;
};
bool cmp(node a, node b)
{
    if(a.tot != b.tot)
        return a.tot > b.tot;
    if(a.ch != b.ch)
        return a.ch > b.ch;
    return a.id < b.id;
}
int main(){
    int n;
    cin >> n;
    node a[n];
    for(int i = 1; i <= n; i++){
        cin >> a[i].ch >> a[i].ma >> a[i].en;
        a[i].tot = a[i].ch + a[i].ma + a[i].en;
        a[i].id = i;
    }
    sort(a + 1, a + n + 1, cmp);
    for(int i = 1; i <= 5; i++)
        cout << a[i].id << ' ' << a[i].tot << endl;
    return 0;
} 

by hierophant_green @ 2019-11-16 10:27:54

真的是玄学


by _jimmywang_ @ 2019-11-16 10:32:50

 node a[n];

放外面,变成

node a[301];

就A了


by _jimmywang_ @ 2019-11-16 10:33:13

like this:

#include<bits/stdc++.h>
using namespace std;
struct node{
    int ch;
    int en;
    int ma;
    int id;
    int tot;
};
bool cmp(node a, node b)
{
    if(a.tot != b.tot)
        return a.tot > b.tot;
    if(a.ch != b.ch)
        return a.ch > b.ch;
    return a.id < b.id;
}
node a[310];//here
int main(){
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i].ch >> a[i].ma >> a[i].en;
        a[i].tot = a[i].ch + a[i].ma + a[i].en;
        a[i].id = i;
    }
    sort(a + 1, a + n + 1, cmp);
    for(int i = 1; i <= 5; i++)
        cout << a[i].id << ' ' << a[i].tot << endl;
    return 0;
} 

by Skyjoy @ 2019-11-16 10:38:18

@周徐吉070930 数组刚好开小了一个,导致RE,建议以后开全局变量,数组范围开到最大下标+10


by cjy2008 @ 2019-11-16 10:38:33

hi周徐吉 加油


by Skyjoy @ 2019-11-16 10:38:52

@周徐吉070930 都要考试了,习惯一定要弄好


by Skyjoy @ 2019-11-16 10:39:03

加油!


by babyec @ 2019-11-16 10:56:02

不要试图使用形同int a[变量]的写法,按预估的最大数组大小进行声明,再习惯性的加上5或者7位长度。

比如题中说n最大是100,那么最好在main函数外面声明:

  int a[107];

by hierophant_green @ 2019-11-16 11:19:44

嗯嗯


by hierophant_green @ 2019-11-16 11:20:20

谢谢大佬们,加油QWQ


| 下一页