为啥老是八十

P1478 陶陶摘苹果(升级版)

丿雨落★花开 @ 2019-11-30 21:57:48


#include <stdio.h>

int main()
{
    int A, b, c, d, n, s;
    scanf("%d %d", &n, &s);
    scanf("%d %d", &A, &b);
    int y[5001] = { 0 };
    int a[5001][2] = { 0 };
    int i, j, p = 0, q = 0, m = 0,x;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < 2; j++)
        {
            scanf("%d", &a[i][j]);
            if ((j == 1) && a[i][j-1] <= A + b) { y[p] = a[i][j]; x = y[p];  p++;
            }
        }
    }
    for (i = 1; i<=p-1; i++)
        for (j = 0; j <= p - 1 - i; j++)
            if (y[j] > y[j + 1]) {
                q = y[j]; y[j] = y[j + 1]; y[j + 1] = q;
             }
    for (i = 0;; i++)
    {
        s = s - y[i];
        if (s >= 0) m++;
        else break;
    }
    printf("%d", m);

    return 0;

}

by 陌上倾城 @ 2019-11-30 22:59:06

m要--吧


by impuk @ 2019-12-01 08:36:36

因为您太巨了所以只有80


by 丿雨落★花开 @ 2019-12-02 18:10:23

@陌上倾城 嗯嗯,谢谢啦,我其实发现是s可能全部减去y【i】后任然大于0 导致无线循环导致错了,加上限制条件就行了


by 丿雨落★花开 @ 2019-12-02 18:10:55

@一只爬行者 大佬见笑了


|