UVA-846
题意:给定两个数a,b,对a累加一个变量t,t初始为1,每次能+1 or -1 or 不变,要求最少经过多少次加t能使a增加到b,最后一个t要为1。
解题思路:因为开头和最后都要是1,可以想到对于递增到最大值 n之前的所有数都是出现2次的。和 sum = 2*( 1+2+3+……+n),枚举n,使得sum>b-a。次数为n*2。有个细节就是如果sum
/*************************************************************************
> File Name: UVA-846.cpp
> Author: Narsh
>
> Created Time: 2016年07月14日 星期四 15时56分37秒
************************************************************************/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int a,b,t,n;
int main () {
scanf("%d",&t);
while (t--) {
scanf("%d%d",&a,&b);
if (a > b) {
n=a;a=b;b=n;
}
a=b-a;
int s=0,ans=0;
n=1;
while (s < a) {
if (s + n >= a){
ans++;
break;
}
s+=2*n;
n++;
ans+=2;
}
printf("%d\n",ans);
}
}