zoukankan      html  css  js  c++  java
  • Uva 846 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
     
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    int main()
    {
        int T, start, end, mid, leave, n, temp, sum;
        scanf("%d", &T);
        while(T--)
        {
            scanf("%d%d", &start, &end);
            leave = (end-start)%2;
            mid = (end-start)/2;
            n = (int)floor(sqrt(2.0*mid));
            if(n*(n+1)/2 - mid != 0) 
            {
                if(n*(n+1)/2 - mid > 0) n--;
                temp = mid - n*(n+1)/2;
                sum = 2*n;
                if(leave)
                {
                    if(temp*2+1 <= n+1) sum++;
                    else if(temp+1 <= n+1 || temp*2 <= n+1 ) sum += 2;
                    else sum += 3;
                }
                else
                {
                    if(temp*2 <= n+1) sum++;
                    else sum += 2;
                }
            } 
            else sum = 2*n+leave;
            printf("%d\n", sum);
        }
        return 0;
    }

    解题报告:

  • 相关阅读:
    JavaBean的详细及引用
    动态页面,登陆,注册,留言 JSP
    简单登陆,注册的动态网页
    11.24作业3
    11.24作业2
    转: JAVA递归算法实例小结
    转: javascript实现全国城市三级联动菜单代码
    转: 我们为什么使用ORM?
    转:Ajax中的get和post两种请求方式的异同
    转: JSTL SQL标签库 使用
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/2941657.html
Copyright © 2011-2022 走看看