题目链接:
https://ac.nowcoder.com/acm/problem/216177
题目大意:
给两个数x和y,问经过多少次异或操作,可以将x变成y,输出操作和异或的数
题目思路:
这题比较简单,但是我想稍微写一写异或的东西。
下面是异或的几个特点:
1、X ^ 0 = X
2、X ^ 1 = ~X
3、X ^ X = 0
4、A ^ (B ^ C) = (A ^ B) ^ C;
5、 A ^ B = C 成立,那么 B ^ C = A;
由这些规律,我们其实可以很轻易地得到这题解法。
直接上代码
代码实现:
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
inline int read(){
int f=0,num;
char ch;
while(ch=getchar(),!isdigit(ch))if(ch=='-')f=1;num=ch-'0';
while(ch=getchar(), isdigit(ch))num=num*10+ch-'0';
return f?-num:num;
}
ll x,y;
int main(){
scanf("%lld%lld",&x,&y);
ll ans=x^y;
if(ans<x){
puts("1");
printf("%lld\n",ans);
}
else if(ans>x){
puts("2");
printf("%lld %lld\n",y,x);
}
return 0;
}