uva846 Steps
很简单的题目,只要最简单的思路就能很高效的解决
View Code
1 #include<stdio.h> 2 int main() 3 { 4 int T, a, b, k, i, r, t; 5 scanf("%d",&T); 6 while(T--) 7 { 8 scanf("%d%d",&a,&b); 9 k = b-a; 10 t = 0; 11 if(a == b) 12 { 13 printf("0\n"); 14 continue; 15 } 16 for(i = 1;;i++) 17 { 18 t += 2*i; 19 if(t > k) 20 { 21 t -= 2*i; 22 i--; 23 break; 24 } 25 } 26 r = 2*i; 27 if(k == t) 28 { 29 printf("%d\n",r); 30 continue; 31 } 32 if(k-t > i+1) 33 { 34 r += 2; 35 printf("%d\n",r); 36 continue; 37 } 38 r += 1; 39 printf("%d\n",r); 40 41 } 42 return 0; 43 }