题目链接:https://vjudge.net/problem/Gym-101170H
解题思路:
如果用一个值给他们做排名,可以发现一个格雷码的值是从高位开始间隔性+,-变化2^(i)-1。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char a[105],b[105];
int main(){
int n;
scanf("%d",&n);
scanf("%s%s",a,b);
ll l = 0,r = 0;
int s = 1;
for(int i = n; i >= 1; i--){
if(a[n-i]-'0')
l += s*((1ll<<(i))-1),s *= -1;
}
s = 1;
for(int i = n; i >= 1; i--){
if(b[n-i]-'0')
r += s*((1ll<<(i))-1),s *= -1;
}
if(l>r)
swap(l,r);
printf("%lld\n",r-l-1);
return 0;
}
标签:格雷,101170H,int,Gym,long,scanf,105 From: https://blog.51cto.com/u_12468613/6384499