您的位置:首页 > 其它

o.boj 1022 Steps

2011-12-17 00:21 337 查看
注:最近这一系列ACM的内容,都是2年多之前的代码,自己回顾一下。

Steps

Submit: 849 Accepted:294
Time Limit: 1000MS Memory Limit: 65536K
Description

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

Input consists of a line containing n, the number of test cases.

Output

For each test case, a line follows with two integers: 0 <= x <= y < 2^31. 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

Source

Waterloo local 2000.01.29

从题目便可以知道,在已知步数的情况下,最多可以走多少的距离。本时不卡时间,直接从步数从少到大计算最长距离是与跟已知条件的距离相等,即可。

#include <stdio.h>
#include <stdlib.h>
main()
{
int count, n, x, y, distance, sum;
int i;

scanf("%d", &n);

for (i = 0; i < n; i++)
{
scanf("%d %d", &x, &y);
distance = y - x;

count = 0;
sum = 0;

while (sum < distance)
{
if (count % 2 == 0)
sum = (count/2 + 1) * count/2;
else
sum = (count/2 + 1) * (count/2 + 1);
count ++;
}
if (count)
count --;
printf("%d\n", count);
}
// system("pause");
return 0;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: