UVa 846 - Steps

来源:互联网 发布:python手册中文版下载 编辑:程序博客网 时间:2024/04/28 23:40
/*若距离s为 n*n, 每步距离为 1,2...n-1,n,n-1...2,1 时,步数必然最小。当s不是平方数时,可求出小于s的最小平方数n*n, 则差值dd = s - n*n < (n+1)*(n+1) - n*n = 2n+1, 故dd<=2n所以 当dd<=n时,则1步可走完; 若n<dd<=2n,则2步可走完*/#include <cstdio>#include <cmath>int main() {    #ifndef ONLINE_JUDGE    freopen("in.txt", "r", stdin);    #endif    int n, x, y;    scanf("%d", &n);    while(n--) {        scanf("%d%d", &x, &y);        int d = y-x;        if(!d) {            printf("0\n");            continue;        } else {            int c = sqrt(d+0.1);            int s = 2*c-1;            if(d-c*c > 0) {                if(d-c*c > c) s += 2;                else ++s;            }            printf("%d\n", s);        }    }    return 0;}

原创粉丝点击