【算法】程序猿不写代码是不对的73
来源:互联网 发布:劫单挑亚索 知乎 编辑:程序博客网 时间:2024/06/06 03:36
package com.kingdz.algorithm.time201706;import java.util.Arrays;/** * <pre> * 步骤问题 * * http://judgecode.com/problems/1006 * * Suppose you start from x = 0. * In each step you can operate on x by either of the 2 operations: * (1) x = x * 2 * (2) x = x + 1 * Given an integer n. How many operations at least to change x into n? * * Input:A single non-negative integer n which is no larger than 2147483647. * Output:The minimum number of operations to change x = 0 into n. * </pre> * * @author kingdz * */public class Algo22 { public static void main(String[] args) { int n = 100; int[] arr = new int[n + 1]; arr[0] = 1; for (int i = 0; i < arr.length; i++) { int now = arr[i]; int a = i * 2; int b = i + 1; if (a < arr.length && arr[a] == 0) { arr[a] = now + 1; } if (b < arr.length && arr[b] == 0) { arr[b] = now + 1; } if (a == n || b == n) { break; } } System.out.println(arr[n] - 1); System.out.println(Arrays.toString(arr)); }}
阅读全文
0 0
- 【算法】程序猿不写代码是不对的73
- 【算法】程序猿不写代码是不对的1
- 【算法】程序猿不写代码是不对的2
- 【算法】程序猿不写代码是不对的3
- 【算法】程序猿不写代码是不对的4
- 【算法】程序猿不写代码是不对的5
- 【算法】程序猿不写代码是不对的6
- 【算法】程序猿不写代码是不对的7
- 【算法】程序猿不写代码是不对的8
- 【算法】程序猿不写代码是不对的9
- 【算法】程序猿不写代码是不对的10
- 【算法】程序猿不写代码是不对的11
- 【算法】程序猿不写代码是不对的12
- 【算法】程序猿不写代码是不对的13
- 【算法】程序猿不写代码是不对的14
- 【算法】程序猿不写代码是不对的15
- 【算法】程序猿不写代码是不对的16
- 【算法】程序猿不写代码是不对的17
- ToBox全球分享理念
- ImageLoader 加载本地图片
- windows7、windows8中mysql5.7的配置my.ini文件在哪
- 第一电动网原域名暂停访问通知!
- Confluence 更改数据库地址
- 【算法】程序猿不写代码是不对的73
- vue2 子组件调用父组件中data中的值
- iOS动态关联对象
- 算数移位(<<, >>)与逻辑移位
- 一篇写的很赞的HTTPS的文章
- echarts折线图实线与虚线拼接,及提示框浮层内容格式的设置
- Linux-execve时的文件加载流程
- jQuery 页面加载完成调用函数
- net use命令详解