Two Buttons
2015-06-11 20:52
309 查看
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button,
device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially,
the display shows number n.
Bob wants to get number m on
the display. What minimum number of clicks he has to make in order to achieve this result?
Input
The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104),
separated by a space .
Output
Print a single number — the minimum number of times one needs to push the button required to get the number m out
of number n.
Sample test(s)
input
output
input
output
Note
In the first example you need to push the blue button once, and then push the red button once.
In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
两个操作,把这个数减一;把这个数乘2。然后求从n到m的最少步骤。自己就是想离散化,然后用队列不断搜索,另外还要把坐标标记一下,避免重复访问,死循环。
然而,可以分析一下,要扩大这个数,只能*2,;减小这个数只能-1.所以如果m小于n,就是n-m。如果m大于n,那就用最小的步骤把m变得小于n。
Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button,
device multiplies the displayed number by two. After clicking the blue button, device subtracts one from the number on the display. If at some point the number stops being positive, the device breaks down. The display can show arbitrarily large numbers. Initially,
the display shows number n.
Bob wants to get number m on
the display. What minimum number of clicks he has to make in order to achieve this result?
Input
The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104),
separated by a space .
Output
Print a single number — the minimum number of times one needs to push the button required to get the number m out
of number n.
Sample test(s)
input
4 6
output
2
input
10 1
output
9
Note
In the first example you need to push the blue button once, and then push the red button once.
In the second example, doubling the number is unnecessary, so we need to push the blue button nine times.
两个操作,把这个数减一;把这个数乘2。然后求从n到m的最少步骤。自己就是想离散化,然后用队列不断搜索,另外还要把坐标标记一下,避免重复访问,死循环。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; int min(int a, int b) { return a < b ? a : b; } int dp[20005], leap[20005]; queue<int>p; int main() { int i, j, m, n, ans, temp, pre; memset(leap, 0, sizeof(leap)); cin >> n >> m; if (m>n)pre = m; else pre = n; pre = pre * 2; for (i = 0; i <= pre; i++) dp[i] = 11000; dp = 0; p.push(n); leap = 1; while (p.size()){ temp = p.front(); if (temp >= 2) { dp[temp - 1] = min(dp[temp - 1], dp[temp] + 1); if (!leap[temp - 1]) { p.push(temp - 1); leap[temp - 1] = 1; } } if (temp * 2 <= 20005) { dp[temp * 2] = min(dp[temp * 2], dp[temp] + 1); if (!leap[temp * 2]) { p.push(temp * 2); leap[temp * 2] = 1; } } p.pop(); } cout << dp[m] << endl; return 0; }
然而,可以分析一下,要扩大这个数,只能*2,;减小这个数只能-1.所以如果m小于n,就是n-m。如果m大于n,那就用最小的步骤把m变得小于n。
#include <iostream> using namespace std; int main(){ int n, m, i = 0; cin >> n >> m; for (i; n < m; i++){ if (m % 2 == 0) m = m / 2; else m++; } cout << i + n - m; return 0; }
相关文章推荐
- Android之AES加密
- Codeforces 549F Yura and Developers
- Android自由选择TextView的文字
- AEC (Acoustic Echo Canceller) 模型
- 场景调研
- 程序员面试宝典9 -STL模板与容器
- GIS空间分析 之基于DEM的梯田规划模拟
- 单元测试概述
- VS2013编译GLUI
- 地方门户网站如何推广
- 快速备份sqlserver2005以上版本数据库的方法-摘自网络
- 【王佩丰】致学员---你和我的距离
- UICamera的触摸事件
- springMVC学习(一)springMVC简介
- Hadoop 学习之MapReduce
- 设置跨域请求头
- 编写makefile生成.ko
- 快速排序 java实现
- Hibernate与MyBatis区别
- 八卦一下黄晓明和Angelababy的电话号码