P1093 [NOIP2007 普及组] 奖学金

P1093 [NOIP2007 普及组] 奖学金

XMDxmd0525 @ 2022-09-03 17:53:00

//这个为啥错了
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
int n, c[301], m[301], e[301], sum[301], z[301];
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d %d %d", &c[i], &m[i], &e[i]);
        sum[i] = c[i] + m[i] + e[i];
        z[i] = sum[i];
    }
    sort(sum + 1, sum + n + 1);
    int a[5] = {0}, j = 1;
    for (int i = n; i >= n - 4; i--) {
        a[j++] = sum[i];
    }
    for (int k = 1; k < j; k++) {
        for (int i = 1; i <= n; i++) {
            if (a[k] == z[i]) {
                printf("%d %d\n", i, a[k]);
                z[i]=0;
                break;
            }
        }
    }

    return 0;
}

by a2lyaXNhbWUgbWFyaXNh @ 2022-09-03 17:58:04

结构体:?

排序的话之前的顺序信息就没了啊


by Katz @ 2022-09-03 18:10:49

楼上正解


by VoldemortSZzqq @ 2022-09-03 18:17:57

用结构体,如果不会的话就需要用冒泡对sum数组进行排序,把和sum数组有关的看成一个整体,假如sum[1]的位置要改变,那么c[1],m[1],e[1]的位置同样要一起改变


by TimelessWelkin @ 2022-09-03 18:21:12

@XMDxmd0525

#include<bits/stdc++.h>
using namespace std;
int n, c[301], m, e, sum[301], z[301];
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%d %d %d", &c[i], &m, &e);
        sum[i] = c[i] + m + e;
        z[i] = i;
    }
    for(int i=1;i<=n-1;i++)
        for(int j=1+i;j<=n;j++)
            if(sum[i]<sum[j]||sum[i]==sum[j]&&c[i]<c[j]||sum[i]==sum[j]&&c[i]==c[j]&&z[i]>z[j]){
                swap(sum[i],sum[j]);
                swap(c[i],c[j]);
                swap(z[i],z[j]);
            }
    for(int i=1;i<=5;i++)printf("%d %d\n",z[i],sum[i]);
    return 0;
}

这个一般用冒泡吧(不知道是因为我不会用快排还是怎么样

然后我把c[]和m[]去掉了,因为只需要读入


by XMDxmd0525 @ 2022-09-03 22:14:46

结构体我忘记咋做了,我只能输出4个,而且我输出的z,a都对,不知道为啥最后就少输出一组,两个样例都少输出了


by XMDxmd0525 @ 2022-09-11 22:22:41

@loushuhao 你这在z[i]不是一直小于z[j]吗


by TimelessWelkin @ 2022-09-14 19:49:27

@XMDxmd0525

???


by jmh_AK_IOI @ 2022-09-14 20:39:41

你这冒泡做的不对


|