Problem 2212 Super Mobile Charger
Accept: 5 Submit: 16
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
2
3 10
100 99 90
3 1000
0 0 0
Sample Output
2
3
Source
//题意:
现在有n个手机,一个电量为m%(百分之m)的万能充电宝
然后输入n个手机的现有电量(百分之a[i]),现在问你这个万能充电宝能把几个手机充满(电量为百分之百)。
//思路:
主要是转化,可以将充电宝的电量看成一个背包。现在要将每个手机所需电量往里面放,最多放几个。
都不用01背包,直接将所需电量排序,在累加直到他们的和大于等于m就结束。。
具体看代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[110];
int main()
{
int t,n,m,x;
int i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(a,0,sizeof(a));
for(i=0;i<n;i++)
{
scanf("%d",&x);
a[i]=100-x;
}
sort(a,a+n);
int sum=0;
for(i=0;i<n;i++)
{
sum+=a[i];
if(sum>m)
break;
}
printf("%d\n",i);
}
return 0;
}