HDOJ 5610 Baby Ming and Weight lifting

Baby Ming and Weight lifting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 143    Accepted Submission(s): 61


Problem Description
Baby Ming is fond of weight lifting. He has a barbell pole(the weight of which can be ignored) and two different kinds of barbell disks(the weight of which are respectively a and b ), the amount of each one being infinite.

Baby Ming prepare to use this two kinds of barbell disks to make up a new one weighted C (the barbell must be balanced), he want to know how to do it.

HDOJ 5610 Baby Ming and Weight lifting_第1张图片
 

Input
In the first line contains a single positive integer T , indicating number of test case.

For each test case:

There are three positive integer a,b , and C .

1T1000,0<a,b,C1000,ab
 

Output
For each test case, if the barbell weighted C can’t be made up, print Impossible.

Otherwise, print two numbers to indicating the numbers of a and b barbell disks are needed. (If there are more than one answer, print the answer with minimum a+b )
 

Sample Input
   
   
   
   
2 1 2 6 1 4 5
 

Sample Output
   
   
   
   
2 2 Impossible 题意:
铭宝宝喜欢举重运动,他有一个杠铃杆(重量忽略),和2种类型的杠铃片(重量分别为ab),每种杠铃片都有无限个。
铭宝宝打算用这2种杠铃片组成重量为C的杠铃(杠铃必须平衡),他想让你告诉他,应该如何组合。

思路:暴力枚举就行了




ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
using namespace std;
int main()
{
    int t,a,b,c;
    int i,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&a,&b,&c);
        int ansa,ansb,sum=INF;
        int bz=0;
        for(i=0;i<=c/a;i++)
        {
            if(i%2)
            continue;
            int num=(c-a*i)/b;
            for(j=0;j<=num;j++)
            {
                if(j%2)
                continue;
                if(a*i+b*j==c)
                {
                    bz=1;
                    if(sum>i+j)
                    {
                        sum=i+j;
                        ansa=i;ansb=j;
                    }
                }
            }
        }
        if(bz)
        printf("%d %d\n",ansa,ansb);
        else
        printf("Impossible\n");
    }
    return 0;
}


你可能感兴趣的:(HDOJ 5610 Baby Ming and Weight lifting)