2014-03-19 05:57

题目:给定一个整数N,求出比N大,而且二进制表示中和N有相同个数的‘1’的最小的数,比如3是‘11’,接下来的5是‘101’,再接下来的6是‘110’。

解法:从低位往高位,首先跳过连续的‘0’,然后跳过连续的‘1’,并数数有多少个1。如果这时还没到最高位,那就从刚才跳过的‘1’中拿出1个放到这位上(当前位是‘0’),然后把剩下的‘1’填到最低的几位上去。中间填充‘0’。比如:‘100111000’的下一个是‘101000011

代码:

 // 5.3 Find the next largest number that have same number of '1's with the given number.
#include <cstdio>
using namespace std; unsigned int findNext(unsigned int n)
{
int n_zero;
int n_one; n_zero = ;
while (n_zero < && (n & ( << n_zero)) == ) {
++n_zero;
}
if (n_zero == ) {
// all 0
return n;
} n_one = n_zero;
while (n_one < && (n & ( << n_one)) != ) {
++n_one;
}
if (n_one == ) {
// no larger number is possible
return n;
} n = n >> n_one << n_one;
n |= ( << n_one);
for (int i = ; i < n_one - n_zero - ; ++i) {
n |= ( << i);
} return n;
} int main()
{
unsigned int n; while (scanf("%u", &n) == ) {
printf("%u\n", findNext(n));
} return ;
}

《Cracking the Coding Interview》——第5章:位操作——题目3的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目1

    2014-03-19 05:45 题目:给定两个数M和N,将N按照二进制位,覆盖到M的特定段位中去. 解法:位操作,请看代码. 代码: // 5.1 Insert one number into th ...

  9. 《Cracking the Coding Interview》——第5章:位操作——题目8

    2014-03-19 06:33 题目:用一个byte数组来模拟WxH的屏幕,每个二进制位表示一个像素.请设计一个画水平线的函数. 解法:一个点一个点地画就可以了.如果要优化的话,其实可以把中间整字节 ...

  10. 《Cracking the Coding Interview》——第5章:位操作——题目6

    2014-03-19 06:24 题目:将一个整数的奇偶二进制位交换,(0, 1) (2, 3) ... 解法:使用掩码来进行快速交换,定义掩码为'0101...'和‘1010...’. 代码: // ...

随机推荐

  1. COGS 2091. Asm.Def的打击序列

    ★★★   输入文件:asm_lis.in   输出文件:asm_lis.out   简单对比时间限制:4 s   内存限制:256 MB [题目描述] 白色圆柱形的“蓝翔”号在虚空中逐渐变大,一声沉 ...

  2. 概念:RPG游戏中两个兵种互相攻击的逻辑

    直接上题目: 解析题目: 根据题目的解析,进行代码的实现: 输出结果: 心得: (1) 当我们面对‘公式结果不是我们想要的’时,应该在脑海里将一个完整的攻击流程进行想象,就会对流程有个更清晰的思路 ( ...

  3. 12/13 exercise

    gcc -[cog] gcc   pro1.o   pro2.o      //create a executable file x.out if unnamed

  4. frame、window和dialog区别

    属性 Window Frame Dialog 模式化 不是 不是 不是(可设置) 可调大小 不可 可 可 标题栏 无 有 有 边界 无 有 有 标题 无 有 有 菜单栏 无 有 无 焦点管理器 有 有 ...

  5. 【转载】#344 - Hidden Base Class Member Is Invoked Based on Declared Type of Object

    When you use the new modifier to hide a base class method, it will still be called by objects whose ...

  6. hdu-2838 Cow Sorting---逆序对的花费

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2838 题目大意: 就是求将之前的排列变成一个递增的排列,每交换两个数的代价为两个数的和,求变成递增的 ...

  7. bpclntcmd一条神奇的命令,解决新安装nbu客户端无法连接的问题 (屡试不爽神命令)

    1. bpclntcmd案例 bpclntcmd -clear_host_cache bpclntcmd – 测试 NetBackup 系统的功能,并在 NetBackup 客户端上启用光纤传输服务 ...

  8. B1954 The xor-longest Path

    爆炸入口 给定一颗带权值的,节点数为n的树,求树上路径最大异或和. solution: 先dfs将所有点到根的异或和算出来.然后放进tire树中贪心. #include<cstdio> # ...

  9. 初尝微信小程序2-基本框架

    基本框架: .wxml :页面骨架 .wxss :页面样式 .js :页面逻辑    描述一些行为 .json :页面配置 创建一个小程序之后,app.js,app.json,app.wxss是必须的 ...

  10. element el-tooltip 内容换行的方法

    <el-tooltip class="item" effect="light" placement="top-start">  ...