Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 628 Solved: 192
Title Description
There are a bunch of sugar Xiaoming, he has n (0 <n <1000) stamina points, there are m candy bars, each candy eaten required strength to the point ai (0 <ai <1000). I ask, how many pieces of candy Xiao Ming up to eat it?
Entry
The first line has two numbers n and m, represent the physical quantity and the second confectionery are the number of rows Xiaoming m, a0, a1, ... a (m-1), representing the physical eat their needs.
Export
The maximum number of pieces of candy to eat.
Sample input
6 3 1 2 3
Sample Output
3
#include<stdio.h>
int main(){
int n,m,k;
int a[1000];
int ai=0,aa;
scanf("%d",&n);
scanf("%d",&m);
for(k=0;k<m;k++){
scanf("%d",&a[k]);
}
int i,j;
for(i=0;i<m;i++){
for(j=0;j<m-i-1;j++){
if(a[j]>a[j+1]){
aa=a[j];
a[j]=a[j+1];
a[j+1]=aa;
}
}
}
while(n>0){
if(n<a[ai]){
break;
}
n=n-a[ai];
ai++;
}
printf("%d",ai);
return 0;
}