题目描述
Consider the process of stepping from integer x to integer y along integer points of the straight line. The length of each step must be non-negative and can be one bigger than, equal to, or 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 both the first and the last step must be 1.
输入
The input begins with a line containing n, the number of test cases. Each test case that follows consists of a line with two integers: 0<=x<=y <231.
输出
For each test case, print a line giving the minimum number of steps to get from x to y
样例输入
3
45 48
45 49
45 50
样例输出
3
3
4
参考代码
#include <stdio.h>
int main()
{
long x,y,n,i;
scanf("%ld",&n);
while(n--)
{
scanf("%ld%ld",&x,&y);
x=y-x;
if(x<=3)
{
printf("%ldn",x);
continue;
}
for(i=2;;i++)
{
if(i*i==x)
{
printf("%ldn",2*i-1);
break;
}
else if(x>i*i&&x<=i*(i+1))
{
printf("%ldn",2*i);
break;
}
else if(x>i*(i+1)&&x<=(i+1)*(i+1))
{
printf("%ldn",2*i+1);
break;
}
}
}
return 0;
}
解析
暂无