您的位置:首页 > 其它

UVA 864-Steps 数学规律

2014-05-21 18:12 676 查看
题意:已知数轴两个整数x,y,你要从一个整数走到另一个整数,如果你某一步走的长度为s,那么你下一步只能走s+1,s或者s-1步,并且第一步和最后一步长度必须是1,求至少要多少步才能从x走到y。

思路:从两边增加,每步都增加1,如果不行了就特判一下。

#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n, x, y;
int res;
int main()
{
scanf ("%d", &n);
while (n--)
{
scanf ("%d%d", &x, &y);
int t = y - x;

if (t == 0) res = 0;
else if (t == 1) res = 1;
else
{
res = 0;
int x = 1;
while (t > 0)
{
if (t >= 2 * x)
{
t -= 2 * x;
res += 2;
x++;
}
else
{
if (t > x)
{
res += 2;
break;
}
else
{
res += 1;
break;
}
}
}
}
printf ("%d\n", res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: