60分,有注释

P1478 陶陶摘苹果(升级版)

ansan @ 2023-12-13 21:53:13


#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n, s, a, b, x[30000], y[20000], i, j, count = 0, sum = 0;
    cin >> n >> s;
    cin >> a >> b;
    for(i = 1; i <= n; i++){
        cin >> x[i] >> y[i];
    }
    for(i = 1; i <= n; i++){
        if(i == n && x[i] > a+b){
            y[i] = 100000;//防止当i == n时无法把y[i]去掉;        
        }
        if(a+b < x[i]){
            for(j = i; j < n; j++)//把数组往前移,同时把y[i]给去掉; 
                x[j] = x[j+1];
            for(j = i; j < n; j++)
                y[j] = y[j+1];
                n--;
        }
    }
        sort(y+1, y+n+1);//用sort进行排序; 
        for(i = 1; i <= n; i++){
            if(sum + y[i] <= s){//条件累加; 
                sum += y[i];
            count++;
            }
        }
        cout << count;
    return 0;
}

by mc_xiexie @ 2023-12-17 15:20:14

@ansan

第一点你没有判断能不能摘到

改完80分

#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n, s, a, b, x[30000], y[20000], i, j, count = 0, sum = 0;
    cin >> n >> s;
    cin >> a >> b;
    for(i = 1; i <= n; i++){
        cin >> x[i] >> y[i];
    }
    for(i = 1; i <= n; i++){
        if(i == n && x[i] > a+b){
            y[i] = 100000;//防止当i == n时无法把y[i]去掉;        
        }
        if(a+b < x[i]){
            for(j = i; j < n; j++)//把数组往前移,同时把y[i]给去掉; 
                x[j] = x[j+1];
            for(j = i; j < n; j++)
                y[j] = y[j+1];
                n--;
        }
    }
        sort(y+1, y+n+1);//用sort进行排序; 
        for(i = 1; i <= n; i++){
            if(sum + y[i] <= s){//条件累加; 
                if(x[i]<a+b){//判断能不能摘到;
                    sum += y[i];
                    count++;
                }
            }
        }
        cout << count;
    return 0;
}

by ansan @ 2023-12-21 13:07:36

@mc_xiexie 大佬然后怎么搞啊,还有哪里出问题了啊 (已关)


|