最后输出这个-1是为什么?自己写的忘记原因了

P1478 陶陶摘苹果(升级版)

_I_GOT_SMOKE_ @ 2024-08-21 16:08:24

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

int main()
{
    int n,s,a,b,t = 1,sum = 0;
    int x,y;
    int k[5001] = {0};
    cin >> n >> s >> a >> b;
    for(int i = 1; i <= n; i++)
    {
        cin >> x >> y;
        if(x <= a + b)
        {
            k[t] = y;
            t++;
        }
    }
    for(int i = t - 1; i >= 1; i--)
    {
        for(int j = 1; j <= i; j++)
        {
            if(k[j] > k[j + 1])
            {
                swap(k[j],k[j + 1]);
            }
        }
    }
    for(int i = 1; i <= t; i++)
    {
        if(s - k[i] >= 0)
        {
            s = s - k[i];
            sum++;
        }
    }   
    cout << sum - 1;
    return 0;
}

by ieouu_Zack @ 2024-08-21 16:22:14

因为if(s - k[i] >= 0)的那一个苹果摘不了


by ieouu_Zack @ 2024-08-21 16:23:38

求关


by _I_GOT_SMOKE_ @ 2024-08-22 13:23:52

@ieouu_Zack okk懂了


by jkh1212 @ 2024-08-24 16:41:04

good


by xiaokang_suancai @ 2024-09-20 22:12:10

@BOT10086 其实排序可以直接用 sort 快排的……


|