求解!

P1478 陶陶摘苹果(升级版)

Haoyixuan11 @ 2023-11-24 22:47:19

呜呜呜,布吉岛哪里错了------------求解!好人一生平安!!!! 我太难了 !!!!


by SDLTF_凌亭风 @ 2023-11-24 22:53:15

你代码呢


by ZWK210917 @ 2023-11-26 16:28:37

你代码会编译错误的原因是因为:你的数组和变量重名了,都叫a。


by ZWK210917 @ 2023-11-26 16:31:06

改成这样就行了:

#include<iostream>
#include<algorithm>
using namespace std;

struct st{
    int g,l;
} c[5005];

bool cmp(st x,st y)
{
    return x.l < y.l;
}

int main()
{
    int n,s,a,b;
    cin>>n>>s>>a>>b;
    for(int i = 1;i <= n;i++)
    {
        cin>>c[i].g>>c[i].l;
    }
    sort(c+1,c+n+1,cmp);
    int cur = 1,ab = a+b,ans = 0;
    for(;cur <= n;cur++)
    {
        if(ab >= c[cur].g&&s>=c[cur].l) 
        {
            s -= c[cur].l;
            ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}

by Haoyixuan11 @ 2024-09-27 17:21:07

感谢@ZWK210917


|