求救

P1093 [NOIP2007 普及组] 奖学金

155555a @ 2023-11-21 19:50:14


#include<iostream>
#include<algorithm>
using namespace std;
#define MAX 310
struct abb
{
    int num = 0;
    double yuwen  = 0;
    double shuxue = 0;
    double yingyu = 0;
    double zong   = 0;
}add[MAX];
bool cmp(abb a, abb B)
{
    if (a.zong > B.zong)
        return a.zong > B.zong;
    if (a.zong == B.zong)
    {
        if (a.yuwen > B.yuwen)
            return a.yuwen > B.yuwen;
        if (a.yuwen == B.yuwen)
            return a.num > B.num;
    }
    else return 0;
}
int main()
{
    int n; cin >> n;
    for (int i = 0; i < n; i++)
    {
        add[i].num = i+1;
        cin >> add[i].yuwen >> add[i].shuxue >> add[i].yingyu;
        add[i].zong = add[i].shuxue + add[i].yingyu + add[i].yuwen;
    }
    sort(add, add + n, cmp);
    for (int j = 0; j < 5; j++)
    {
        cout << add[j].num << " " << add[j].zong << endl;
    }
    return 0;
}
//为什么只有80分,哪里错了,求求大佬教教

by C_ccx_N @ 2023-11-21 19:58:09

#include<bits/stdc++.h>
using namespace std;

const int MAX = 1010;

struct stu{
    int s,ch,ma,eng;
    int id;
}p[MAX];

bool cmp(stu a,stu b){
    if(a.ch == b.ch && a.s == b.s)
    return a.id < b.id;
    if(a.s == b.s)
    return a.ch>b.ch;
    return a.s > b.s;

}

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>p[i].ch>>p[i].ma>>p[i].eng;
        p[i].id = i+1;
        p[i].s = p[i].ch + p[i].ma + p[i].eng;
    }
    sort(p,p+n,cmp);
    for(int i=0;i<5;i++)
    cout<<p[i].id<<' '<<p[i].s<<endl;
}

by flh2011 @ 2023-11-29 19:48:22

他需要的是解决问题,你直接发代码干嘛


by flh2011 @ 2023-11-29 19:50:45

问题是在cmp里,判断学号大小时,是学号小的在前,应改成<

#include<iostream>
#include<algorithm>
using namespace std;
#define MAX 310
struct abb
{
    int num = 0;
    double yuwen  = 0;
    double shuxue = 0;
    double yingyu = 0;
    double zong   = 0;
}add[MAX];
bool cmp(abb a, abb B)
{
    if (a.zong > B.zong)
        return a.zong > B.zong;
    if (a.zong == B.zong)
    {
        if (a.yuwen > B.yuwen)
            return a.yuwen > B.yuwen;
        if (a.yuwen == B.yuwen)
            return a.num < B.num;
    }
    else return 0;
}
int main()
{
    int n; cin >> n;
    for (int i = 0; i < n; i++)
    {
        add[i].num = i+1;
        cin >> add[i].yuwen >> add[i].shuxue >> add[i].yingyu;
        add[i].zong = add[i].shuxue + add[i].yingyu + add[i].yuwen;
    }
    sort(add, add + n, cmp);
    for (int j = 0; j < 5; j++)
    {
        cout << add[j].num << " " << add[j].zong << endl;
    }
    return 0;
}

by flh2011 @ 2023-11-29 20:00:36

@155555a
重新改了一下,还有cmp里不是a.zong > B.zong,改成!=就行了

#include<iostream>
#include<algorithm>
using namespace std;
#define MAX 310
struct abb
{
    int num = 0;
    double yuwen  = 0;
    double shuxue = 0;
    double yingyu = 0;
    double zong   = 0;
}add[MAX];
bool cmp(abb a, abb B)
{
    if (a.zong!=B.zong)
        return a.zong > B.zong;
    if (a.zong == B.zong)
    {
        if (a.yuwen!=B.yuwen)
            return a.yuwen > B.yuwen;
        if (a.yuwen == B.yuwen)
            return a.num < B.num;
    }
    else return 0;
}
int main()
{
    int n; cin >> n;
    for (int i = 0; i < n; i++)
    {
        add[i].num = i+1;
        cin >> add[i].yuwen >> add[i].shuxue >> add[i].yingyu;
        add[i].zong = add[i].shuxue + add[i].yingyu + add[i].yuwen;
    }
    sort(add, add + n, cmp);
    for (int j = 0; j < 5; j++)
    {
        cout << add[j].num << " " << add[j].zong << endl;
    }
    return 0;
}

|