You are given two positive integers a and b

You are given two positive integers a and b.
In one move, you can change a in the following way:
Choose any positive odd integer x (x>0) and replace a with a+x;
choose any positive even integer y (y>0) and replace a with a−y.
You can perform as many such operations as you want. You can choose the same numbers x and y in different moves.
Your task is to find the minimum number of moves required to obtain b from a. It is guaranteed that you can always obtain b from a.
You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases.
Then t test cases follow. Each test case is given as two space-separated integers a and b (1≤a,b≤109).

Output
For each test case, print the answer — the minimum number of moves required to obtain b from a if you can perform any number of moves described in the problem statement. It is guaranteed that you can always obtain b from a.
在这里插入图片描述

#include <iostream>
#include <cstdio>
using namespace std;
int pd(int a,int b){
    if(a==b){
        return 0;
    } else if (a<b){
        if((a%2==0&&b%2==0)||(a%2!=0&&b%2!=0)){
            return 2;
        } else {
            return 1;
        }
    } else{
        if((a%2==0&&b%2==0)||(a%2!=0&&b%2!=0)){
            return 2;
        } else {
            return 1;
        }
    }
}
int main() {
    int n,aa,bb;
    scanf("%d",&n);
    while (n--){
        scanf("%d %d",aa,bb);
        printf("%d\n",pd(aa,bb));
    }
}

发布了136 篇原创文章 · 获赞 18 · 访问量 4200

猜你喜欢

转载自blog.csdn.net/xcdq_aaa/article/details/105057335