规律就是两个数字的level差+left差+right差
代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 int main() 8 { 9 int a,b; 10 while(~scanf("%d%d",&a,&b)) 11 { 12 int i1,i2,j1,j2; 13 int left1,left2,right1,right2; 14 i1 = sqrt(a-1)+1;j1 = a-(i1-1)*(i1-1); 15 i2 = sqrt(b-1)+1;j2 = b-(i2-1)*(i2-1); 16 left1 = j1/2+j1%2; left2 = j2/2+j2%2; 17 right1 = (2*i1-j1)/2+(2*i1-j1)%2; right2 = (2*i2-j2)/2+(2*i2-j2)%2; 18 int ans = abs(i1-i2)+abs(left1-left2)+abs(right1-right2); 19 // printf("i1 = %d; i2 = %d; j1 = %d; j2 = %d; left1 = %d; left2 = %d; right1 = %d; right2 = %d; ",i1,i2,j1,j2,left1,left2,right1,right2); 20 printf("%d ",ans); 21 } 22 return 0; 23 }