请问为什么RE,求大佬解释

P1093 [NOIP2007 普及组] 奖学金

sxyn @ 2019-11-03 09:13:12

include<iostream>

include<cstdio>

include<cmath>

include<algorithm>

using namespace std; struct student{ int num; int yw; int sx; int yy; int s; }; student a[61]; int cmp(student a,student b) { return (a.s>b.s)|| (a.s==b.s&&a.yw>b.yw)|| (a.s==b.s&&a.yw==b.yw&&a.num<b.num); } int main() { int n; cin>>n; for(int i=0;i<n;i++) { a[i].num=i+1; cin>>a[i].yw>>a[i].sx>>a[i].yy; a[i].s=a[i].yw+a[i].sx+a[i].yy; }
sort(a,a+n,cmp); for(int i=0;i<5;i++) cout<<a[i].num<<" "<<a[i].s<<endl; return 0; }


by 花园Serena @ 2019-11-03 09:14:21

请不要使用头文件来强调MD的重要性。。


by 花园Serena @ 2019-11-03 09:18:42

#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std; 
struct student  {
    int num, yw, sx, yy, s;
}a[61];
int cmp(student a,student b) { return (a.s > b.s) || (a.s == b.s && a.yw > b.yw) ||  (a.s == b.s && a.yw == b.yw && a.num < b.num); } 
int main() { 
    int n; cin>>n;
    for(int i = 0; i < n; i ++)  {
        a[i].num = i + 1; 
        cin>>a[i].yw>>a[i].sx>>a[i].yy; a[i].s=a[i].yw+a[i].sx+a[i].yy; 
    }
        sort(a, a + n, cmp);
    for(int i = 0;i < 5;i ++)
        cout<<a[i].num<<" "<<a[i].s<<endl;
    return 0;
}

by 花园Serena @ 2019-11-03 09:20:39

@sxyn 您的问题应该是数组越界,学生人数 < 300,您只定义了61


by 花园Serena @ 2019-11-03 09:22:04

#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std; 
struct student  {
    int num, yw, sx, yy, s;
}a[300 + 10];
int cmp(student a,student b) { return (a.s > b.s) || (a.s == b.s && a.yw > b.yw) ||  (a.s == b.s && a.yw == b.yw && a.num < b.num); } 
int main() { 
    int n; cin>>n;
    for(int i = 0; i < n; i ++)  {
        a[i].num = i + 1; 
        cin>>a[i].yw>>a[i].sx>>a[i].yy; a[i].s=a[i].yw+a[i].sx+a[i].yy; 
    }
        sort(a, a + n, cmp);
    for(int i = 0;i < 5;i ++)
        cout<<a[i].num<<" "<<a[i].s<<endl;
    return 0;
}

by 花园Serena @ 2019-11-03 09:22:19

这样应该就好啦


by sxyn @ 2019-11-03 09:34:03

@Van♂様年华 谢谢dalao谢谢dalao


|