有没有dalao帮忙看一下怎么错的

P1478 陶陶摘苹果(升级版)

ATRI @ 2019-08-22 15:22:35

#include<bits/stdc++.h>
using namespace std;
struct tao
{
    int xi;
    int yi;
}c[5100];
bool cmp(tao a,tao b)
{
    return a.yi<b.yi;
}
int main()
{int n,a,s,b,sum=0;
cin>>n>>s>>a>>b;
for(int i=1;i<=n;i++)
{
 cin>>c[i].xi;
 cin>>c[i].yi;  
}
sort(c+1,c+n+1,cmp); 
for(int i=1;i<=n;i++)
{
    if(a+b>=n&&s>c[i].yi)
    {
       sum++;
       s-=c[i].yi;
    }
}
cout<<sum;
    return 0;
}

by huang_yue @ 2019-08-22 15:33:16

if(a+b>=n&&s>c[i].yi)

改为

if(a+b>=c[i].xi&&s>c[i].yi)

by huang_yue @ 2019-08-22 15:33:40

不对,是改为

if(a+b>=c[i].xi&&s>=c[i].yi)

by zmx200767 @ 2019-08-22 16:31:01

#include<bits/stdc++.h>
using namespace std;
struct tao
{
    int xi;
    int yi;
}c[5100];
bool cmp(tao a,tao b)
{
    return a.yi<b.yi;
}
int main()
{int n,a,s,b,sum=0;
cin>>n>>s>>a>>b;
for(int i=1;i<=n;i++)
{
 cin>>c[i].xi;
 cin>>c[i].yi;  
}
sort(c+1,c+n+1,cmp); 
for(int i=1;i<=n;i++)
{
    if(a+b>=c[i].xi&&s>=c[i].yi)
    {
       sum++;
       s-=c[i].yi;
    }
}
cout<<sum;
    return 0;
}

by chenpengda @ 2019-08-24 14:13:33

我做的AC题解


|