A - JB Loves Math

该博客讨论了一种数学问题解决方法,其中涉及选择一个正奇数x和正偶数y来通过增加或减少给定数字a,逐步将其转化为目标数字b。在最少的操作次数内完成这个转变是问题的关键。文章给出了一个C++代码示例来演示如何确定最小操作数,并解释了不同情况下的操作计数逻辑。
摘要由CSDN通过智能技术生成

SDUT 2022 Spring Team Contest(for 21) - 12 - Virtual Judge 

JB is good at Math, so he thinks all the math problems in the world are easy.

But one day, he meets a math problem which he can't solve, so he asks you to help him.

JB will give you two numbers aa and bb, and you should then choose a positive odd number xx and a positive even number yy. You can let aa add xx or let aa minus yy in one operation. You should change aa into bb in the minimal number of operations. Note that you are not allowed to change the value of xx and yy.

Input

In the first line, there is one integer TT (1\leq T\leq 10^51≤T≤105), denoting the number of test cases.

For each test case, there is one line containing two numbers aa and bb (1\leq a,b\leq 10^61≤a,b≤106), denotes the number given by JB.

Output

For each test case, print one number, denoting the minimal number of operations you need to change aa into bb.

Sample 1

InputcopyOutputcopy
2
3 6
5 3
1
1

题意:给定两个正整数 a, b,你需要选定一个正奇数 x 和一个正偶 数 y。 之后的每一步操作中,你可以将 a 增大 x 或者将 a 减小 y。 求把 a 变成 b 的最少操作次数。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int a,b;
        scanf("%d %d",&a,&b);
        if(a==b)printf("0\n");
        else if(a<b)
        {
            if((b-a)%2==1)printf("1\n");//这种好理解
            else if((b-a)/2%2==1)printf("2\n");//类比第一种,b-a如果被分
                                               //了两半能够为奇数,就是2,否则就是3
            else printf("3\n");
            //首先必须明确结果只可能为1,2,3;
        }
        else
        {
            //同理,结果只可能为1,2
            if((a-b)%2==0)printf("1\n");
            else
                printf("2\n");
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值