Accept: 890 Submit: 1697
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
While HIT ACM Group finished their contest in Shanghai and is heading back Harbin, their train was delayed due to the heavy snow. Their mobile phones are all running out of battery very quick. Luckily, zb has a super mobile charger that can charge all phones.
There are N people on the train, and the i-th phone has p[i] percentage of power, the super mobile charger can charge at most M percentage of power.
Now zb wants to know, at most how many phones can be charged to full power. (100 percent means full power.)
Input
The first line contains an integer T, meaning the number of the cases (1 <= T <= 50.).
For each test case, the first line contains two integers N, M(1 <= N <= 100,0 <= M <= 10000) , the second line contains N integers p[i](0 <= p[i] <= 100) meaning the percentage of power of the i-th phone.
Output
For each test case, output the answer of the question.
Sample Input
Sample Output
Source
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)题目大意:有一个电量为m的电池,给n个手机充电,最多可以充满多少个
题解:想要冲更多的手机,当然要先冲那些需要电量少的手机啦,什么也不说啦,先按照手机充满需要的电量进行排序,遍历数组,m减少对应的值,看看能充满多少个手机
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int t,n,m,ans;
int a[105];
cin>>t;
while(t--){
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>a[i];
a[i]=100-a[i];
}
sort(a,a+n);
ans=0;
for(int i=0;i<n;i++){
if(m<=0)break;
m-=a[i];
if(m>=0)ans++;
}
cout<<ans<<endl;
}
return 0;
}