1 条题解

  • 0
    @ 2026-9-30 12:59:26

    C++ :

    
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    static int a[105];
    /* 求最多件数 ⇒ 取重量最小的若干件,直到装不下为止。 */
    int main(){
        int n;
        long long x;
        if(scanf("%d %lld", &n, &x) != 2) return 0;
        for(int i = 0; i < n; i++) scanf("%d", &a[i]);
        sort(a, a + n);
        long long sum = 0;
        int cnt = 0;
        for(int i = 0; i < n; i++){
            if(sum + a[i] <= x){ sum += a[i]; cnt++; }
            else break;
        }
        printf("%d\n", cnt);
        return 0;
    }
    
    
    • 1

    信息

    ID
    2854
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者