问题描述
地上有一排西瓜,每个西瓜都有自己的重量。淘淘有一个包,包的容量是固定的,淘淘希望尽可能在包里装更多的西瓜(当然要装整个的,不能切开装),请问淘淘的包最多能装下多少个西瓜?
输入
第一行两个整数 n,x,表示有 n 个西瓜,背包容量是x 。(1<=n<=100) 下面 n个整数数,表示西瓜的重量。
输出
一个整数,表示淘淘最多能装多少西瓜回家。
样例
5 10
2 3 1 5 4
输出
4
#include <bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int a[110],n,i,x,cnt=0,num=0;
cin>>n>>x;
for(i=1;i<=n;i++) cin>>a[i] ;
sort(a+1,a+n+1);
for(i=1;i<=n;i++)
{
cnt+=a[i];
if(cnt<=x) num++;
}
cout<<num;
return 0;
}
制作不易,能点个赞吗,感谢!!