萌新求解!!!

P1478 陶陶摘苹果(升级版)

ziqikang @ 2024-11-08 19:19:29

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

int n,s,a,b;
struct node{
    int x,y;
}apple[6000];

int cmp(node e,node h) {
    return e.y < h.y;
}

int main()
{
    cin >> n >> s;
    cin >> a >> b;
    for (int i = 1;i <= n;i++) {
        cin >> apple[i].x >> apple[i].y;
    }
    sort(apple + 1,apple + n + 1,cmp);
    int sum = 0;
    bool ans = 1;
    int i = 1;
    while (ans != 0 && i <= n){
        if (s >= apple[i].y && a + b >= apple[i].x) {
            s = s - apple[i].y;
            sum++;
            i++;
        }
        else{
            ans = 0;
        }
    }
    cout << sum;
    return 0;
}

by SSqwq_ @ 2024-11-08 19:27:33

@ziqikang 帮你改好了。去除意义不明的 ans 变量,将指针 i 的移动放在 if 外面即可。

关注 SS 谢谢喵。

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

int n,s,a,b;
struct node{
    int x,y;
}apple[6000];

int cmp(node e,node h) {
    return e.y < h.y;
}

int main()
{
    cin >> n >> s;
    cin >> a >> b;
    for (int i = 1;i <= n;i++) {
        cin >> apple[i].x >> apple[i].y;
    }
    sort(apple + 1,apple + n + 1,cmp);
    int sum = 0;
    int i = 1;
    while (s > 0 && i <= n){
        if (s >= apple[i].y && a + b >= apple[i].x) {
            s = s - apple[i].y;
            sum++;
        }
        i++;
    }
    cout << sum;
    return 0;
}

by ziqikang @ 2024-11-08 19:43:18

@SSqwq_ 谢谢,已经关了


|