您的位置:首页 > 其它

寒假第一天 水题 Steps

2013-01-20 09:23 246 查看
                                                                                                                                           Steps

Time Limit: 3000MS Memory limit: 65536K

题目描述

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 consists of a line containing n, the number of test cases. For each test case, a line follows
with two integers: 0<= x <= y < 231

输出

For each test case, print a line giving the minimum number of steps to get from
xto y.

示例输入

3
45 48
45 49
45 50


示例输出

3
3
4

 

 

 

题目:从x到y 可以比前一步大一小一或者等于前一步的步长 但是第一步和最后一步都只能是1

求的是最短用的步数

思路:

从两边走第一次相差的距离两边同时减一 第二次两边的距离同时减2 第三次减三…… 当值出现负值的时候 就是最少路长~


 

 

 

 

 

 

 

 

 

 

 
#include<iostream>

using namespace std;
int main()
{
int s,e;
int n;
cin>>n;
while(n--)
{
cin>>s>>e;
int len=e-s;
if(len==0)
cout<<0<<endl;
else if(len==1)
cout<<1<<endl;
else if(len==2)
cout<<2<<endl;
else
{
len-=2;
int bushu=3;
for(int i=2;;i++)
{
len-=i;
if(len<=0)
break;
bushu++;
len-=i;
if(len<=0)
break;
bushu++;

}
cout<<bushu<<endl;

}

}
return 0;
}


 

 

 

 

 

 

 

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