uva 648 steps

Steps
PC/UVa IDs: 110608/846, Popularity: A, Success rate: high Level: 2
Consider the process of stepping from integer x to integer y along integer points of
the straight line. The length of each step must be non-negative and can be one bigger
than, equal to, or 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
both the first and the last step must be 1.
Input
The input begins with a line containing n, the number of test cases. Each test case that
follows consists of a line with two integers: 0 ≤ x ≤ y < 231 .
Output
For each test case, print a line giving the minimum number of steps to get from x to y.
Sample Input
3
45 48
45 49
45 50
Sample Output
3
3
4



等差數列!!!


#include <cstdio>
#include <cmath>

int main () {
    int cs;
    scanf ("%d", &cs);
    while (cs--) {
        long long a, b;
        scanf ("%lld%lld\n", &a, &b);
        long long s = b - a;
        long long n = llrint (floor (sqrt (0.25 + s) - 0.5));
        if (n * (n + 1) == s) {
            printf ("%lld\n", n + n);
        } else if (n * (n + 1) + n + 1 >= s) {
            printf ("%lld\n", n + n + 1);
        } else {
            printf ("%lld\n", n + n + 2);
        }
    }
    return 0;
}




發佈了94 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章