Input
The problem contains many test cases, please process to the end of file( EOF ).
Each test case contains two lines.
In the first line, there are two integers L(0<L<1000000000) and N(0<=N<600) which
mentioned above.
In the second line, there are N positive integers. The i th integer Ai(0<Ai<1000000000 ) means that the i th block has the size of 1×Ai (in inch).
Each test case contains two lines.
In the first line, there are two integers L(0<L<1000000000) and N(0<=N<600) which
mentioned above.
In the second line, there are N positive integers. The i th integer Ai(0<Ai<1000000000 ) means that the i th block has the size of 1×Ai (in inch).
Output
For each test case , print an integer which represents the minimal number of blocks are needed.
If Kitty could not repair the wall, just print "impossible" instead.
If Kitty could not repair the wall, just print "impossible" instead.
Sample Input
5 3 3 2 1 5 2 2 1
Sample Output
2 impossible 问题描述:
多次输入,分两行,第一行有两个数,第一个是需要的长度,第二个是现有木材的个数,第二行是现有木材的长度,如果现有木材满足需要输出最少的木材数目,否则输出impossible
#include <iostream> #include<algorithm> using namespace std; bool cmp(int a,int b) { return a>b; } int main() { int L,a[601],n,i,sum,num; while(cin>>L>>n) { for(i=0; i<n; i++) cin>>a[i]; sort(a,a+n,cmp); num=sum=0; for(i=0; i<n; i++) if(sum<L) { sum+=a[i]; num++; } else break; if(sum<L) cout<<"impossible"<<endl;
else cout<<num<<endl;
} return 0; }