40分跪求神犇们帮助

P1478 陶陶摘苹果(升级版)

ihan255 @ 2018-09-04 00:02:17

//为什么错了呀 4(yu)0(ku)分(wu)呀(lei)

include<iostream>

include <algorithm>

using namespace std; int main() { int n,s,a,b,x,y,i,k[5005],sum=0; cin>>n>>s>>a>>b; a+=b; for(i=1;i<=n;i++) { cin>>x>>y; if(x<=a) k[i]=y; } sort(k+1,k+n+1); for(i=1;i<=n&&(s-k[i])>0;i++) { if(k[i]==0) continue; s-=k[i]; sum++; } cout<<sum; return 0; }


by ihan255 @ 2018-09-04 00:03:03

我整理了一下

#include<iostream>
#include <algorithm>
using namespace std;
int main()
{
    int n,s,a,b,x,y,i,k[5005],sum=0;
    cin>>n>>s>>a>>b;
    a+=b;
    for(i=1;i<=n;i++) 
    {
    cin>>x>>y;
    if(x<=a) k[i]=y;
    }
    sort(k+1,k+n+1);
    for(i=1;i<=n&&(s-k[i])>0;i++) 
    {
    if(k[i]==0) continue;
    s-=k[i];
    sum++;
    }
    cout<<sum;
    return 0;
}

by 文文殿下 @ 2018-09-04 07:28:51

你的代码,最后循环,应该改为 s-k[i]>=0 否则忽略了体力恰好用完的情况。

不谢。

参考代码

#include<cstdio>
#include<algorithm>
const int maxn = 5010;
int n,s,a,b,y[maxn],tot=0;
int main() {
    scanf("%d%d%d%d",&n,&s,&a,&b);
    a+=b;
    for(register int i = 1;i<=n;++i) {
        int t1,t2;
        scanf("%d%d",&t1,&t2);
        if(t1<=a) y[++tot]=t2;
    }
    std::sort(y+1,y+1+tot);
    int ans = 0;
    for(register int i = 1;i<=tot&&s-y[i]>=0;++i) {
        ans++;
        s-=y[i];
    }
    printf("%d",ans);
    return 0;
}

by ihan255 @ 2018-09-04 21:57:01

哦,谢谢大神啦


by ihan255 @ 2018-09-04 21:59:35

@文文殿下 大神,还是错了呀```cpp

include<iostream>

include <algorithm>

using namespace std; int main() { int n,s,a,b,x,y,i,k[5005],sum=0; cin>>n>>s>>a>>b; a+=b; for(i=1;i<=n;i++) { cin>>x>>y; if(x<=a) k[i]=y; } sort(k+1,k+n+1); for(i=1;i<=n&&(s-k[i])>0;i++) { if(k[i]==0) continue; s-=k[i]; sum++; } cout<<sum; return 0; }


by ihan255 @ 2018-09-04 22:00:28

@文文殿下 大神,还是错了呀

#include<iostream>
#include <algorithm>
using namespace std;
int main()
{
    int n,s,a,b,x,y,i,k[5005],sum=0;
    cin>>n>>s>>a>>b;
    a+=b;
    for(i=1;i<=n;i++) 
    {
    cin>>x>>y;
    if(x<=a) k[i]=y;
    }
    sort(k+1,k+n+1);
    for(i=1;i<=n&&(s-k[i])>0;i++) 
    {
    if(k[i]==0) continue;
    s-=k[i];
    sum++;
    }
    cout<<sum;
    return 0;
}

by ihan255 @ 2018-09-04 22:01:59

@文文殿下 我改了后是60分


by ihan255 @ 2018-09-04 22:03:32

@文文殿下 刚才粘贴错了

#include<iostream>
#include <algorithm>
using namespace std;
int main()
{
    int n,s,a,b,x,y,i,k[5005],sum=0;
    cin>>n>>s>>a>>b;
    a+=b;
    for(i=1;i<=n;i++) 
    {
    cin>>x>>y;
    if(x<=a) k[i]=y;
    }
    sort(k+1,k+n+1);
    for(i=1;i<=n&&(s-k[i])>=0;i++) 
    {
    if(k[i]==0) continue;
    s-=k[i];
    sum++;
    }
    cout<<sum;
    return 0;
}

by 文文殿下 @ 2018-09-05 07:07:53

k没初始化。 文文记得局部变量是随缘的QAQ


by 文文殿下 @ 2018-09-05 07:08:24

@wu55555


by ihan255 @ 2018-09-05 22:13:31

@文文殿下 谢谢啦QAQ


| 下一页