题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2124
Ac code :
#include<stdio.h>
#include<stdlib.h>
int cmp(const void *a,const void *b)
{
return *(int *)b-*(int *)a;
}
int main()
{
int L, N, i;
int a[601], sum, len;
while(scanf("%d",&L)!=EOF)
{
scanf("%d",&N);
for(i = 0; i < N; i++)
scanf("%d",&a[i]);
qsort(a, N, sizeof(a[0]), cmp);
sum = len = 0;
for(i = 0; i < N; i++)
{
if(len < L)
{
len += a[i];
sum++;
}
else
break;
}
if(len < L)
printf("impossible\n");
else
printf("%d\n",sum);
}
return 0;
}