题目大意:
一个人沿直线走路,第一步和最后一步必须是1,每次走的长度也必须是正的,而且比上一步长度多或少一或相等,求最短步数。
解题思路:
让x=sqrt(dis),如果dis=x^2,则count=2*n-1;如果dis-x^2<n,多加一步;如果dis-x^2>n,多加两步。
细节注意:
当dis小于3的时候要单独处理,既输出dis。
AC代码:
//#define Local
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
#ifdef Local
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif
int n = 0, x = 0, a = 0, b = 0, dis = 0, count = 0;
cin >> n;
while (n--)
{
count = 0;
cin >> a >> b;
dis = b - a;
if (dis <= 3)
cout << dis << endl;
else
{
x = sqrt(dis);
if (x*x == dis)
cout << 2*x-1 << endl;
else if (dis - x*x <= x)
cout << 2*x << endl;
else
cout << 2*x+1 << endl;
}
}
}