joj2154 Hungry Rabbits

 2154: Hungry Rabbits


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K1231423Standard
A group of hungry rabbits had got a basket of carrots, but these carrots isn't enough for all the rabbits to stay alive. So they had a meeting to solve the problem. They determined to drive off some rabbits,then the rest of them could have enough carrots. The number of carrots each rabbit required is given as a nonegetive interger v i, and there's T carrots in the basket. The chairman of rabbit association is a big beautiful and strong rabbit, he was very pity for doing this thing, and he wanted to know the least number of rabbits they would drive off to make the rest of the rabbits stay alive.

Input

There's more than one test case.In each case, there're two positive integers T and N. T stands for the number of carrots in the basket and N(1<=N<=1000) stands for the total number of the rabbits. Then following N lines each contains a nonegetive interger v i. These N intergers stand for the carrots each rabbit needed.

Output

For each test case ,output the least number of rabbits they would drive off!

Sample Input

30 5
12
14
23
4
10

Sample Output

2

 

Problem Source: sea

 


This problem is used for contest: 23 


Submit / Problem List / Status / Discuss

 

WA了一次,忘了考虑输出为0(即sum<=t)的情况。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int cmp(const void *a, const void *b)
 5 {
 6     return *(int *)a - *(int *)b;
 7 }
 8 
 9 
10 int main()
11 {
12     int t, n, i, sum;
13     int a[1005];
14     
15     while (scanf("%d%d", &t, &n) == 2)
16     {
17         sum = 0;
18         for (i=0; i<n; ++i)
19         {
20             scanf("%d", &a[i]);
21         }
22         qsort(a, n, sizeof(a[0]), cmp);
23         
24         for (i=0; i<n; ++i)
25         {
26             sum += a[i];
27             if (sum > t)
28             {
29                 printf("%d\n", n-i);
30                 
31                 break;
32             }
33             else if (i == n-1 && sum <= t)
34             {
35                 printf("0\n");
36             }
37         }
38     }
39     
40     
41     return 0;
42 }

 

转载于:https://www.cnblogs.com/RootJie/archive/2012/05/03/2481493.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值