弱鸡求救,问题好像在排序cmp函数里,但是我怎么改都不对!!!!!!!

P1093 [NOIP2007 普及组] 奖学金

Michelle01 @ 2023-07-10 19:15:47

我的辣鸡代码,dalao别嫌弃

谢谢dalao

// c++从入门到入坟.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include<iostream>
#include <stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;

int c[100], m[100], e[100];

struct node{

    int ch;
    int math;
    int engilsh; 
    int w;
    int m;
    int he; 
} a[100], o[100];

bool cmp(node x, node y){
    if(x.empty == y.empty){
        return x.ch > y.ch;
        if(x.ch == y.ch){
            return x.w < y.m;
        }
    }
    return x.empty > y.empty;
}

int main(){

    int n;
    cin >> n;
    int max = 0;
    for(int i = 1; i <= n; i++){
        cin >> o[i].ch >> o[i].math >> o[i].engilsh;
        o[i].w = i;
        o[i].empty = o[i].ch + o[i].math + o[i].engilsh; 
    }

    sort(o + 1, o + 1 + n, cmp);

    for(int i = 1; i <= 5; i++){
        cout << o[i].w  << " " << o[i].empty << endl;
    }
    return 0;
}

by Henry2012 @ 2023-07-10 19:24:22

好像没定义empty呀


by Michelle01 @ 2023-07-10 19:39:41

@Henry2012 奥是对,我这个弱鸡粗心,谢谢daolao

但是,我提交了之后还是有问题,有三个点RE

// c++从入门到入坟.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include<iostream>
#include <stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;

int c[100], m[100], e[100];

struct node{

    int ch;
    int math;
    int engilsh; 
    int w;
    int m;
    int he; 
    int empty;
} a[100], o[100];

bool cmp(node x, node y){
    if(x.empty == y.empty){
        return x.ch > y.ch;
        if(x.ch == y.ch){
            return x.w < y.m;
        }
    }
    return x.empty > y.empty;
}

int main(){

    int n;
    cin >> n;
    int max = 0;
    for(int i = 1; i <= n; i++){
        cin >> o[i].ch >> o[i].math >> o[i].engilsh;
        o[i].w = i;
        o[i].empty = o[i].ch + o[i].math + o[i].engilsh; 
    }

    sort(o + 1, o + 1 + n, cmp);

    for(int i = 1; i <= 5; i++){
        cout << o[i].w  << " " << o[i].empty << endl;
    }
    return 0;
}

by Henry2012 @ 2023-07-10 20:12:01

数组开大点,开到107就行了


by Henry2012 @ 2023-07-10 20:13:00

int a[100];的下标范围是0~99


by Henry2012 @ 2023-07-10 20:13:57

@Michelle01


by Michelle01 @ 2023-07-10 20:18:03

@Henry2012 欧克呀,谢谢大佬,以后多多学习你


by Henry2012 @ 2023-07-11 07:50:07

@Michelle01 互关吧


|