这破题,根本看不懂题意啊。。。题意:一棵中序遍历是1 2 3 4 5...的满二叉树,从a a+1 a+2 a+3 b,总共多少步。x到y的距离为中间有多少个点。a > b没注意2Y。
1 #include <cstring> 2 #include <cstdio> 3 #include <string> 4 #include <iostream> 5 #include <algorithm> 6 #include <cmath> 7 #include <map> 8 using namespace std; 9 int bin[60]; 10 int judge(int x) 11 { 12 int i,temp; 13 if(x <= 0) 14 return 0; 15 for(i = 31;i >= 0;i --) 16 { 17 if(x&(1<<i)) 18 { 19 temp = x-(1<<i); 20 if(temp) 21 return bin[i] + judge(temp) + i - 1; 22 else 23 return bin[i]; 24 } 25 } 26 return 0; 27 } 28 int main() 29 { 30 int i,a,b; 31 bin[0] = bin[1] = 0; 32 for(i = 2;i <= 31;i ++) 33 { 34 bin[i] = 2*bin[i-1] + i-1; 35 } 36 scanf("%d%d",&a,&b); 37 if(a > b) 38 swap(a,b); 39 printf("%d ",judge(b)-judge(a)); 40 return 0; 41 }