UVA846 - Steps

找规律可以发现一个数列 (0),1,2,4,6,9,12,16,20....分别是1步,2步,3步,4步....的最大距离。然后把x-y锁定到一个区间就行了。注意距离为0的时候走0步。

题目:

Steps

 

  Steps 

One steps through integer points of the straight line. The length of a step must be nonnegative and can be by one bigger than, equal to, or by one smaller than the length of the previous step.

What is the minimum number of steps in order to get from x to y? The length of the first and the last step must be 1.

 

Input and Output 

Input consists of a line containing n, the number of test cases. For each test case, a line follows with two integers:

0$ \le$x$ \le$y < 231. For each test case, print a line giving the minimum number of steps to get from x to y.

 

Sample Input 

3
45 48
45 49
45 50

 

Sample Output 

3
3
4

 

 


Miguel Revilla 2002-06-15

 

解答:

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int t,x,y;
 5     scanf("%d",&t);
 6     while(t--)
 7     {
 8         int dis,i,p,s;
 9         scanf("%d%d",&x,&y);
10         dis=y-x;
11         if(dis==0)
12             printf("0\n");
13         else
14         {
15             s=0;
16             i=1;
17             p=1;
18             for(;;)
19             {
20                 s+=p;
21                 if(s>=dis)
22                     break;
23                 if(i%2==0)
24                     p++;
25                 i++;
26             }
27             printf("%d\n",i);
28         }
29     }
30     return 0;
31 }

 

posted @ 2013-03-02 18:40  上白泽慧音  阅读(273)  评论(0编辑  收藏  举报