您的位置:首页 > 其它

hdu 1030 Delta-wave

2015-02-19 19:15 471 查看
题目链接: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
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: