o.boj 1022 Steps

注:最近这一系列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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值