若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1541: Steps

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,174

题目描述

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;
}

解析

暂无

hustoj

发表评论 取消回复

电子邮件地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

Dreams孤独患者 Skip 原站点
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist