Codeforces Round #624 (Div. 3)A. Add Odd or Subtract Even

A. Add Odd or Subtract Even

题目链接-A. Add Odd or Subtract Even
在这里插入图片描述
在这里插入图片描述
题目大意
给你两个数a,b,你就可以通过以下方式更改a:
选择任意正奇数x(x>0),将a替换为a+x;
选择任意正偶数y(y>0),将a替换为a-y。
您可以执行任意数量的此类操作。求从a中得到b所需的最少移动次数
解题思路
判断a,b大小求差值c=abs(a-b),奇+奇=偶,奇+偶=奇,偶+偶=偶

  • 若a==b,无需处理,直接输出0
  • 若a<b,若c是奇数,处理1次,加上1个奇数;c是偶数,处理2次,加上2个奇数。
  • 若a>b, 分两种,若c是偶数,处理1次,减去1个偶数;c是奇数,处理2次,减去1个偶数,加上1个奇数。

附上代码

#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=1e5+5;
typedef long long ll;
typedef pair<int,int> PII;
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	int t;
	cin>>t;
	while(t--){
		int a,b;
		cin>>a>>b;
		if(a==b){
			cout<<0<<endl;
			continue;
		}
		if(a<b){
			if((b-a)%2==1)
				cout<<1<<endl;
			else
				cout<<2<<endl;
		}
		else{
			if((a-b)%2==0)
				cout<<1<<endl;
			else
				cout<<2<<endl;
		}
	} 
	return 0;
}


下面是大佬的代码,还是大佬的代码简洁orz

#include <cstdio>
int main(){
	int t;
	scanf("%d", &t);
	while(t--){
		int a,b;
		scanf("%d%d", &a, &b);
		puts(a==b?"0":(a<b&&(!((b-a)&1)))||(a>b&&((a-b)&1))?"2":"1");
	}
	return 0;
}
发布了78 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Fiveneves/article/details/104501038