杭电5562之Clarke and food

来源:互联网 发布:java工程师年龄要求 编辑:程序博客网 时间:2024/06/11 06:34

Description

Clarke is a patient with multiple personality disorder. One day, Clarke turned into a cook, was shopping for food. 
Clarke has bought  food. The volume of the th food is . Now Clarke has a pack with volume . He wants to carry food as much as possible. Tell him the maxmium number he can brought with this pack.
 

Input

The first line contains an integer , the number of the test cases. 
For each test case: 
The first line contains two integers 
The second line contains  integers, the th integer denotes .
 

Output

For each test case, print a line with an integer which denotes the answer.
 

Sample Input

13 51 3 4
 

Sample Output

2Hint: We can carry 1 and 3, the total volume of them is 5.
 题意:输入n件食物的体积,和背包总体积,问最多能带多少件食物

分析:sort快速排序

AC代码如下:
#include "iostream"#include<algorithm>using namespace std;int main(int argc, char* argv[]){    _int64 N,i,n;    _int64 m,sum;    _int64 a[100001];    scanf("%d",&N);    while(N--)    {        sum=0;        scanf("%I64d%I64d",&n,&m);        for (i=0;i<n;i++)        {            scanf("%I64d",&a[i]);        }        sort(a,a+n);        for(i=0;i<n;i++)        {            sum+=a[i];            if (sum>=m)            {                break;            }        }        printf("%I64d\n",i);    }    return 0;}




0 0