题目:
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 and output
Input consists of a line containing n, the number of test cases. Foreach test case, a line follows with two integers: 0xy < 231.For each test case, print a line giving the minimum number of steps toget from x to y.
sample input
3
45 48
45 49
45 50
sample output
3
3
4
题解:找规律。
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main() {
int n, start, end, dis;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d %d", &start, &end);
dis = end - start;
if (dis == 0) {
printf("0\n");
continue;
}
int a = sqrt(dis);
int b = sqrt(dis) + 1;
if (a * a == dis) {
int ans = a * 2 - 1;
printf("%d\n", ans);
continue;
}
else {
int ans = a * 2;
if (((1 + a) * a) >= dis)
printf("%d\n", ans);
else
printf("%d\n", ans + 1);
continue;
}
}
return 0;
}