ZOJ 3706 Break Standard Weight(暴力)

Description

The balance was the first mass measuring instrument invented. In itstraditional form, it consists of a pivoted horizontal lever of equal lengtharms, called the beam, with a weighing pan, also called scale, suspended fromeach arm (which is the origin of the originally plural term "scales"for a weighing instrument). The unknown mass is placed in one pan, and standardmasses are added to this or the other pan until the beam is as close toequilibrium as possible. The standard weights used with balances are usuallylabeled in mass units, which are positive integers.

Withsome standard weights, we can measure several special masses object exactly,whose weight are also positive integers in mass units. For example, with twostandard weights 1 and 5, we can measure the object with mass 1, 4, 5 or 6 exactly.

In thebeginning of this problem, there are 2 standard weights, which masses are x and y. Youhave to choose a standard weight to break it into 2 parts, whose weights arealso positive integers in mass units. We assume that there is no mass lost. Forexample, the origin standard weights are 4 and 9, if you break thesecond one into 4 and 5, you could measure 7 specialmasses, which are 1, 3, 4, 5, 8, 9, 13. While if you break the first one into 1 and 3, youcould measure 13 special masses, which are 1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12, 13! Your task is to find out the maximum number ofpossible special masses.

Input

Thereare multiple test cases. The first line of input is an integer T < 500 indicatingthe number of test cases. Each test case contains 2 integers x and y. 2 ≤ x, y ≤ 100

Output

For eachtest case, output the maximum number of possible special masses.

Sample Input

2

4 9

10 10

Sample Output

13

9



#include <iostream>

#include <string.h>

#include <math.h>

using namespace std;

int result[500],cnt=0;

int GUI[27][3]={{-1,-1,-1},{-1,-1,0},{-1,-1,1},{-1,0,-1},{-1,0,0},{-1,0,1},{-1,1,-1},{-1,1,0},{-1,1,1},

               {0,-1,-1},{0,-1,0},{0,-1,1},{0,0,-1},{0,0,0},{0,0,1},{0,1,-1},{0,1,0},{0,1,1},

               {1,-1,-1},{1,-1,0},{1,-1,1},{1,0,-1},{1,0,0},{1,0,1},{1,1,-1},{1,1,0},{1,1,1},};

void iscun(int n)

{

       if(n==0)

      return;

       n=fabs(n);

       intflag=1;

       for(inti=0;i<cnt;i++)

         if(result[i]==n)

         {

             flag=0;

             break;

         }

       if(flag==1)

         result[cnt++]=n;

}

int main()

{

   int T;

   cin>>T;

   while(T--)

    {

      int x,y,maxs=2;

      cin>>x>>y;

      for(int k=1;k<=y/2;k++)

      {

          int y1=k,y2=y-k;

           memset(result,0,sizeof(result));

           cnt=0;

           for(int i=0;i<27;i++)

           {

                 int sum=x*GUI[i][0]+y1*GUI[i][1]+y2*GUI[i][2];

               iscun(sum);

           }

          if(cnt>maxs)

            maxs=cnt;

      }

      for(int k=1;k<=x/2;k++)

      {

          int x1=k,x2=x-k;

           memset(result,0,sizeof(result));

           cnt=0;

           for(int i=0;i<27;i++)

           {

                   intsum=x1*GUI[i][0]+x2*GUI[i][1]+y*GUI[i][2];

                   iscun(sum);

           }

          if(cnt>maxs)

            maxs=cnt;

      }

      cout<<maxs<<endl;

    }

       return 0;

}

转载于:https://www.cnblogs.com/MisdomTianYa/p/6581817.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值