求助#5WA,真的不知道怎么错的

P1478 陶陶摘苹果(升级版)

shc0215 @ 2019-10-09 22:37:20

#include<stdio.h> 

int i,a,b,s,n,x,y,k,cnt=0;
int c[10000];
int f(x,y,s,k){
    if(k==2*n)return cnt;
    if(a+b>=x&&s-y>=0){
        cnt++;
        s-=y;
    }
    x=c[k+1];
    y=c[k+2];

    f(x,y,s,k+=2);
}
int main()
{int j,tran,con;
    scanf("%d%d%d%d",&n,&s,&a,&b);
    for(i=1;i<=2*n;i++){
        scanf("%d",&c[i]);
        }
    if(c[1]==0){
        printf("%d",c[1]);
        return 0;
    }
    for(i=0;i<2*n;i+=2){
        for(j=2;j<2*n-i;j+=2){
            if(c[j]>c[j+2]){
                tran=c[j];
                c[j]=c[j+2];
                c[j+2]=tran;
                con=c[j-1];
                c[j-1]=c[j+1];
                c[j+1]=con;

            }
        }
    }

    f(c[1],c[2],s,2);
    printf("%d",cnt);
    return 0;   
}

by Ricardo_21 @ 2019-10-09 23:03:18

#include<bits/stdc++.h>
using namespace std;
int x[10010],y[10010];
int n,s,a,b,i=0,t=0;
int main(){
    cin>>n>>s>>a>>b;
    for(int i=1;i<=n;i++)
        cin>>x[i]>>y[i];
    for(i=1;i<n;i++)
        for(int j=i+1;j<=n;j++)
            if(y[i]>y[j]){
                swap(y[i],y[j]);
                swap(x[i],x[j]);
            }
        i=1;
    while(i<=n&&s>=0){
        if(a+b>=x[i]&&s-y[i]>=0){
            t++;
            s-=y[i];
        }
        i++;
    }
    cout<<t<<endl;
    return 0;
}

by Ricardo_21 @ 2019-10-09 23:03:45

满分解法


by shc0215 @ 2019-10-10 09:17:10

我终于发现原来是我定义的数组元素不够啊,心情复杂


by shc0215 @ 2019-10-10 09:17:53

@回忆似水年华 谢谢


by Ricardo_21 @ 2019-10-13 22:10:24

@shc0215 写代码尽量要多开十到二十位,防止爆掉


|