On a broken calculator that has a number showing on its display, we can perform two operations:

  • Double: Multiply the number on the display by 2, or;
  • Decrement: Subtract 1 from the number on the display.

Initially, the calculator is displaying the number X.

Return the minimum number of operations needed to display the number Y.

Example 1:

  1. Input: X = 2, Y = 3
  2. Output: 2
  3. Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.

Example 2:

  1. Input: X = 5, Y = 8
  2. Output: 2
  3. Explanation: Use decrement and then double {5 -> 4 -> 8}.

Example 3:

  1. Input: X = 3, Y = 10
  2. Output: 3
  3. Explanation: Use double, decrement and double {3 -> 6 -> 5 -> 10}.

Example 4:

  1. Input: X = 1024, Y = 1
  2. Output: 1023
  3. Explanation: Use decrement operations 1023 times.

Note:

  1. 1 <= X <= 10^9
  2. 1 <= Y <= 10^9

CF原题,我们反过来看,Y是偶数就/2,奇数就加1,到了比X小就再加

  1. class Solution {
  2. public:
  3. int brokenCalc(int X, int Y) {
  4. int cnt=;
  5. if(X>=Y){
  6. return X-Y;
  7. }
  8. while(X!=Y){
  9. if(Y<X)Y++;
  10. else if(Y%){
  11. Y++;
  12. }else{
  13. Y/=;
  14. }
  15. //cout<<Y<<endl;
  16. cnt++;
  17. }
  18. return cnt;
  19. }
  20. };

123th LeetCode Weekly Contest Broken Calculator的更多相关文章

  1. 123th LeetCode Weekly Contest Add to Array-Form of Integer

    For a non-negative integer X, the array-form of X is an array of its digits in left to right order.  ...

  2. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  3. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  4. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  5. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  6. 【LeetCode】991. Broken Calculator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  8. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  9. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

随机推荐

  1. nyoj746 整数划分

    nyoj746 http://acm.nyist.net/JudgeOnline/problem.php?pid=746 一道区间dp的题目: 设:a[i][j]为那一串数字中从第i位到第j位的数是多 ...

  2. zigbee广播通信原理

    广播:可以理解成,发送模块发出数据,这个网络里的所有节点模块都可以拿到这个数据. 实验:终端模块以广播的形式发送出去,让协调器和路由器模块作为接收器收到数据并显示出来! 协调器模块作为接收模块: 和单 ...

  3. ajax中 XmlHttp的open( )方法

    博客分类:  Ajax XML  open 创建一个新的http请求,并指定此请求的方法.URL以及验证信息 语法 oXMLHttpRequest.open(bstrMethod, bstrUrl, ...

  4. [GO]无缓冲通道(unbuffered channel)

    无缓冲通道(unbuffered channel)是指在接收前没有能力保存任何值的通道,在之前的例子中使用的都是无缓冲通道,需要注意的是,对于无缓冲通道而言,不管是往通道里写数据还是从通道里读数据,都 ...

  5. eclipse Multiple annotations found at this line

    参考:http://blog.csdn.net/li396864285/article/details/42745071 这样的错不影响编程,是eclipse的校验问题 去掉相应的Build就行了

  6. 练习题。对DOM中document的深刻理解巩固

    //window.onload = modTwo;     1.点击单元格内容  弹窗promrt接收值   将接受的值提换单元格内容    2.点击单元格  出现2个按钮 加粗 字体颜色标红     ...

  7. ExecutorService的submit方法的坑

    先看一段代码: public Future<?> submit(Runnable task) { if (task == null) throw new NullPointerExcept ...

  8. 从Entity Framework的实现方式来看DDD中的repository仓储模式运用

    一:最普通的数据库操作 static void Main(string[] args) { using (SchoolDBEntities db = new SchoolDBEntities()) { ...

  9. 自定义spring valid方式实现验证

    推荐:http://blog.csdn.net/xulianboblog/article/details/51694924

  10. Word页面去除下划线(Office 2017)实现

    后面就不用说了吧设置边框无