B. Two Buttons
来源:互联网 发布:面板数据计量经济学 编辑:程序博客网 时间:2024/05/19 16:51
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?
The first and the only line of the input contains two distinct integers n and m (1 ≤ n, m ≤ 104), separated by a space .
Print a single number — the minimum number of times one needs to push the button required to get the number mout of number n.
4 6
2
10 1
9
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,蓝色:-1,如果变成负数,就变成原来的数。思路是当然是*2可以掠过的步数更少啦,如果n是输入,m是输出。如果n大于m,不能使用红色按钮,很容易看出,步数就是n-m。如果n比m小,如果m是偶数的话,m/2,如果m是奇数,m+1,这样一直循环判断n是不是还比m小,不符合就跳出循环,进入第一个如果。暴力循环即可。
#include<stdio.h>#include <string.h>int main(){int n,m,a=0;scanf("%d %d",&n,&m);while(n<m){if(m%2==0) {m=m/2;}else{m++;}a++;}printf("%d\n",a+n-m);return 0;}
- B. Two Buttons
- CODEFORCES:B. Two Buttons
- B. Two Buttons
- CodeForces 520B【Two Buttons】
- Codeforces 520B. Two Buttons
- Codeforces 520B.Two Buttons
- B. Two Buttons(Codeforces Round #295(div2))
- Codeforces Round #295 B. Two Buttons
- Codeforces 520B. Two Buttons spfa
- Codeforces 520 B Two Buttons BFS
- Codeforces 520B Two Buttons 【BFS】||【spfa】
- Codeforces 295 Div2 B.Two Buttons
- codeforces 520B Two Buttons BFS
- cf 520 B. Two Buttons bfs
- Codeforces 520B Two Buttons 题解
- CodeForces 520 B.Two Buttons(bfs)
- Two Buttons
- Two Buttons
- Phpstorm常用快捷键
- iOS DeepLinkKit使用简介
- configure详解
- asterisk 外网映射连接不上
- Javascript模块化编程(一):模块的写法
- B. Two Buttons
- 链表分割
- Javascript模块化编程(二):AMD规范
- 黑马程序员——Java 面向对象(上)
- 编译db2 clt文件
- Javascript模块化编程(三):require.js的用法
- 开发学习网页
- 50款经典 iPhone 开源应用(附代码下载)
- 小胖说事26------cocoaPods安装以及问题:Setting up CocoaPods master repo