UVA 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: 0xy < 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
规律推出来的
1 1
1 1 2
1 1 1 3
1 2 1 4
1 2 1 1 5
1 2 2 1 6
1 2 1 2 1 7
1 2 2 2 1 8
1 2 3 2 1 9
从这里可以知道
1 1
2 1
3 2
4 2
5 3
6 3
7 4
8 4
所以先求出t,在一次减去个数,直到减去的数<=0,则可以说明他在哪个区间了
#include<stdio.h>

int main()

{

    int x,y;

    int T;

    scanf("%d",&T);

    while(T--)

    {

        scanf("%d%d",&x,&y);

        int t=x-y;

        if(t<0) t=-t;

        if(t==0) printf("0\n");//这里没有考虑WA了一次

        else if(t==1) printf("1\n");

        else if(t==2) printf("2\n");

        else

        {

            t-=2;

            int cas=3;

            for(int i=2;;i++)

            {

                t-=i;

                if(t<=0) break;

                cas++;

                t-=i;

                if(t<=0) break;

                cas++;

            }

            printf("%d\n",cas);

        }

    }

    return 0;

}

你可能感兴趣的:(uva)