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/