http://poj.org/problem?id=2590
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1871
题目大意:
给两个点a,b,要求从a移动到b,每次移动步数要比前面的一步大或者等于或者小1。求最小的步数。(起始和终点必须步数为1)
思路:
直接对称着来做。
比如:
1->7的话
可以 1 2 2 1
1->9
1 2 3 2 1
50
1 2 3 4 5 6 7 6 5 4 3 2 1 1
两个OJ输入格式不一样。。
下面是POJ的:
#include<cstdio>
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int a,b;
scanf("%d%d",&a,&b);
if(a==b)
{
printf("0\n");
continue;
}
int target=b-a;
int sum=0;
int step=1;
int ans=0;
while(sum*2+2*step<target)
{
sum+=step;
step++;
ans+=2;
}
if(target-2*sum <=step)
ans++;
else
ans+=2;
printf("%d\n",ans);
}
return 0;
}
ZOJ:
#include<cstdio>
int main()
{
int a,b;
while(~scanf("%d%d",&a,&b))
{
if(a==b)
{
printf("0\n");
continue;
}
int target=b-a;
int sum=0;
int step=1;
int ans=0;
while(sum*2+2*step<target)
{
sum+=step;
step++;
ans+=2;
}
if(target-2*sum <=step)
ans++;
else
ans+=2;
printf("%d\n",ans);
}
return 0;
}