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. For each test case, a line follows with two integers: 0Sample Input
3 45 48 45 49 45 50
Sample Output
3 3 4 1 1 2 1 1 3 1 1 1 4 1 2 1 5 1 2 1 1 6 1 2 2 1 7 1 2 2 1 1 8 1 2 2 2 1 9 1 2 3 2 1 10 1 2 3 2 1 1 11 1 2 3 2 2 1 12 1 2 3 3 2 1 13 1 2 3 3 2 1 1 14 1 2 3 3 2 2 1 15 1 2 3 3 3 2 1 16 1 2 3 4 3 2 1 规律看了很久才看出来 找出有变化的是5 7 10 13 5 和 10都在2 × 2, 3 × 3之后 观察4 9 16都是2×sqrt() - 1 56 10 11 12 是偶数 sqrt(5) sqrt(6) = 2 《=2×2 + 2 sqrt(10) 11 13 = 3 《=3×3 +3 都是2 × sqrt() 789 13 14 15是奇数 2×sqrt ()+ 1 代码如下#include <stdio.h> #include <math.h> int main(){ int n, step = 0, cha, s; long long x, y; scanf("%d", &n); while (n--) { scanf("%lld%lld", &x, &y); cha = y - x; if (cha <= 3) step = cha; else { s = sqrt(cha); if (s * s == cha) step = 2 * s - 1; else if (cha - s * s <= s) step = 2 * s; else step = 2 * s + 1; } printf("%d\n", step); } return 0; }