hdu 1030 Delta-wave

来源:互联网 发布:淘宝网手机版 编辑:程序博客网 时间:2024/06/02 13:10

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1030

题目大意:计算从a到b最少需要几步

题目分析:

从水平层数上计算:


6在第3层,12在第4层

从左边层数上计算:


6在第2层,12在第3层

从右边层数上计算:


6在第1层,12在第2层

代码参考:

#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int N = 1e5;const int INF = 2e9;int power[N];int cnt = 0;void init() //计算平方数{    int i;    for(i = 0; ; ++ i) {        if(i * i > INF) break;        power[i] = i * i;    }    cnt = i;}int main(){    init();    int n, m;    while(cin >> n >> m) {        int Level1 = lower_bound(power, power + cnt, n) - power; //计算水平层数        int Level2 = lower_bound(power, power + cnt, m) - power;        int Left1 = (n - power[Level1 - 1] + 1) / 2; //计算左边层数        int Left2 = (m - power[Level2 - 1] + 1) / 2;        int Right1 = (power[Level1] - n + 2) / 2; //计算右边层数        int Right2 = (power[Level2] - m + 2) / 2;        int ans = abs(Level1 - Level2) + abs(Left1 - Left2) + abs(Right1 - Right2);        cout << ans << endl;    }    return 0;}


0 0
原创粉丝点击