Accept: 217 Submit: 402 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
题目意思不难理解,注意要排序,因为要取得最大数量的满电的电池
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int i,t,n,m,a[1005];
cin>>t;
while(t--)
{
cin>>n>>m;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);//降序排序
long long t=0;
for(i=0;i<n;i++)
{
m-=100-a[i];
if(m<0)//注意判断条件的位置
break;
t++;
}
cout<<t<<endl;
}
return 0;
}
Accept: 191 Submit: 608 Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
Two different circles can have at most four common tangents.
The picture below is an illustration of two circles with four common tangents.
Now given the center and radius of two circles, your job is to find how many common tangents between them.
Input
The first line contains an integer T, meaning the number of the cases (1 <= T <= 50.).
For each test case, there is one line contains six integers x1 (−100 ≤ x1 ≤ 100), y1 (−100 ≤ y1 ≤ 100), r1 (0 < r1 ≤ 200), x2 (−100 ≤ x2 ≤ 100), y2 (−100 ≤ y2 ≤ 100), r2 (0 < r2 ≤ 200). Here (x1, y1) and (x2, y2) are the coordinates of the center of the first circle and second circle respectively, r1 is the radius of the first circle and r2 is the radius of the second circle.
Output
For each test case, output the corresponding answer in one line.
If there is infinite number of tangents between the two circles then output -1.
Sample Input
Sample Output
要考虑这几种情况:相离(外 4)(内 0)、外切(3)、相交(2)、内切(1)、重合(无数个 即-1)。
用条件语句要注意顺序
#include<iostream>
using namespace std;
int main()
{
int flagd,flagr,t,r1,r2,x1,x2,y1,y2;
cin>>t;
while(t--)
{
cin>>x1>>y1>>r1>>x2>>y2>>r2;
flagd=((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));//两圆心的距离
flagr=(r1+r2)*(r1+r2);// 外离
if(flagd==0&&r1==r2)//重合
cout<<"-1"<<endl;
else if(flagd<(r1-r2)*(r1-r2))//内含
cout<<"0"<<endl;
else if(flagd==(r1-r2)*(r1-r2))//内切
cout<<"1"<<endl;
else if(flagd<flagr)//相交
cout<<"2"<<endl;
else if(flagd==flagr)//外切
cout<<"3"<<endl;
else//外离
cout<<"4"<<endl;
}
return 0;
}
Accept: 83 Submit: 457 Time Limit: 3000 mSec Memory Limit : 32768 KB
Problem Description
Given a set of n items, each with a weight w[i] and a value v[i], determine a way to choose the items into a knapsack so that the total weight is less than or equal to a given limit B and the total value is as large as possible. Find the maximum total value. (Note that each item can be only chosen once).
Input
The first line contains the integer T indicating to the number of test cases.
For each test case, the first line contains the integers n and B.
Following n lines provide the information of each item.
The i-th line contains the weight w[i] and the value v[i] of the i-th item respectively.
1 <= number of test cases <= 100
1 <= n <= 500
1 <= B, w[i] <= 1000000000
1 <= v[1]+v[2]+...+v[n] <= 5000
All the inputs are integers.
Output
For each test case, output the maximum value.
Sample Input
Sample Output
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define INF 0xf3f3f3f
using namespace std;
struct Money
{
int w;
int v;
}aver[5005];
int dp[5005];
int main()
{
int t,n,m;
while(~scanf("%d",&t))
{
while(t--)
{
cin>>n>>m;
int sum=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&aver[i].w,&aver[i].v);
sum+=aver[i].v;
}
memset(dp,INF,sizeof(dp));
dp[0]=0;
for(int i=0;i<n;i++)
for(int j=sum;j>=aver[i].v;j--)
dp[j]=min(dp[j],dp[j-aver[i].v]+aver[i].w);
for(int i=sum;i>=0;i--)
{
if(dp[i]<=m)
{
printf("%d\n",i);
break;
}
}
}
}
return 0;
}