UVa 846 Steps (mathematics)

Source: Internet
Author: User
Tags diff printf time limit

846-steps

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=99&page=show_problem &problem=787

One steps through integer points of the straight line. The length of a step must is nonnegative and can is 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 the the "the" and the last step must is 1.

Input and Output

Input consists of a line containing n, the number of test cases. For each test case, the a line follows with twointegers:0xy <2. For the all test case, print a line giving the minimum number of steps to -to y.

Sample Input

3
45 50

Sample Output

3
3
4

I count the number of steps 9~16, the answer is to see it.

Complete code:

/*0.015s*/
  
#include <cstdio>
#include <cmath>
  
int main (void)
{
    int t, x, Y, diff, N;
    scanf ("%d", &t);
    while (t--)
    {
        scanf ("%d%d", &x, &y);
        diff = y-x;
        if (diff = = 0)
            puts ("0");
        else
        {
            n = (int) sqrt (diff);
            diff-= n * N;
            if (diff = = 0)
                printf ("%d\n", (n << 1)-1);
            else if (diff <= N)
                printf ("%d\n", n << 1);
            else
                printf ("%d\n", (n << 1) + 1);
        }
    return 0;
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.