友谊赛的题目( FZU 2212)( FZU 2213)( FZU 2214)

 FZU 2212
 
 Problem A Super Mobile Charger

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

2 3 10 100 99 90 3 1000 0 0 0

 Sample Output

2 3


题目意思不难理解,注意要排序,因为要取得最大数量的满电的电池

代码如下:

#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;
}
FZU 2213

  

 Problem B Common Tangents

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

3 10 10 5 20 20 5 10 10 10 20 20 10 10 10 5 20 10 5

 Sample Output

4 2 3
  这个题目的意思就是要求两个圆的公共切线,学过数学的人对这个都不陌生。

要考虑这几种情况:相离(外  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;
}

FZU 2214

 Problem C Knapsack problem

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

1 5 15 12 4 2 2 1 1 4 10 1 2

 Sample Output

15
  这个题目是背包类型的,开始以为是贪心,wa了啊。代码wa了,错误还没找出来,气人,结构体为嘛不行,细节吗?补充,bug已经找了出来,代码正确如下:

#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值