现在的位置: 首页 > 综合 > 正文

846 – Steps

2018年01月12日 ⁄ 综合 ⁄ 共 907字 ⁄ 字号 评论关闭

  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 xto y.

Sample Input 

3
45 48
45 49
45 50

Sample Output 

3
3
4

大意:给你两个数,让你判断从x到y需要走多少步,而每一个步走的长度必须是前一个步长度加1,减1,或者和它相同。而且第一步和最后一步的长度为1.

大致如下:

    1
1步   最大差1 1^2

1 1
2步   最大差2

    1
    2      1 3步   最大差4
2^2

1 2
2 1
4步   最大差6

   1     2    3
    2     1 5步   最大差9
3^2

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main ()
{
    int t,x,y,n;
    cin>>t;
    while(t--)
    {
        int d;
        cin>>x>>y;
        d=y-x;
        n=sqrt(d*1.0);
        if (d==0) cout<<"0"<<endl;
        else if (n*n==d) cout<<2*n-1<<endl;
        else if (n*n+n>=d) cout<<2*n<<endl;
        else cout<<2*n+1<<endl;
    }
    return 0;
}

抱歉!评论已关闭.