题目描述

The binary weight of a positive  integer is the number of 1's in its binary representation.for example,the decmial number 1 has a binary weight of 1,and the decimal number 1717 (which is 11010110101 in binary) has a binary weight of 7.Give a positive integer N,return the smallest integer greater than N that has the same binary weight as N.N will be between 1 and 1000000000,inclusive,the result is guaranteed to fit in a signed 32-bit interget.

输入

The input has multicases and each case contains a integer N.

输出

For each case,output the smallest integer greater than N that has the same binary weight as N.

样例输入

1717
4
7
12
555555

样例输出

1718
8
11
17
555557

题解:

1717(0110 1011 0101),下一位是 1718(0110 1011 0110)

767(0010 1111 1111),下一位是 895(0011 0111 1111)

348(0001 0101 1100),下一位是 355(0001 0110 0011)

其中不难发现一个规律,从右起的第一个“01”改变为“10”,并且在“01”的后面所有的“1”都移动至最后,事实上,这个就是解题的关键点,那么整个问题求解的核心就转移到这两个子问题:

1. 将右起第一个“01”,改变为“10”

2. 将该“01”后面的所有“1”移动至最后

 #include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <vector>
#include<string>
#include<cstring>
#include<string.h>
#include<set>
#include<queue>
#include<stack>
#include<map>
using namespace std;
typedef long long LL; int main()
{
int n;
while(cin>>n)
{
int b=n&(-n);// 得到n中最后一个‘1’到最后的值
int t=n+b; // 产生进位, 实现‘01’ 变成 ‘10’
int s=t^n; // 得到进位中发生变动的位
int k=(s>>)/b;//先 ‘01’->'10'发生后不要去改变这两位,所以右移2位,然后将‘01’以后的‘1’移动到最后。
int n=t|k; // 最后的结果就是t|k
cout<<n<<endl;
}
return ;
}

Same binary weight (位运算)的更多相关文章

  1. uestc 1709 Binary Operations 位运算的灵活运用

    Binary Operations Time Limit: 2000 ms Memory Limit: 65535 kB Solved: 56 Tried: 674   Description   B ...

  2. [HDU] 3711 Binary Number [位运算]

    Binary Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  3. 位运算 UEST 84 Binary Operations

    题目传送门 题意:所有连续的子序列的三种位运算计算后的值的和的期望分别是多少 分析:因为所有连续子序列的组数有n * (n + 1) / 2种,所以要将他们分类降低复杂度,以ai为结尾的分成一组,至于 ...

  4. 【LeetCode】位运算 bit manipulation(共32题)

    [78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...

  5. PHP中的位运算与位移运算(其它语言通用)

    /* PHP中的位运算与位移运算 ======================= 二进制Binary:0,1 逢二进1,易于电子信号的传输 原码.反码.补码 二进制最高位是符号位:0为正数,1为负数( ...

  6. C语言中的位运算和逻辑运算

    这篇文章来自:http://blog.csdn.net/qp120291570/article/details/8708286 位运算 C语言中的位运算包括与(&),或(|),亦或(^),非( ...

  7. A - Subarrays Beauty gym 位运算 &

    You are given an array a consisting of n integers. A subarray (l, r) from array a is defined as non- ...

  8. 说说Java 位运算

    前言 我们都知道,在计算机世界里,再复杂,再美的程序,到最后都会变成0与1.也就是我们常说的:二进制.二进制相信大家都很熟悉.与现实世界不同的是,在现实世界里,我们通常都是用十进制来表示的,也就是遇十 ...

  9. Codeforces 868D Huge Strings - 位运算 - 暴力

    You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...

随机推荐

  1. mapreduce程序来实现分类

    文件的内容例如以下所看到的: 5 45 8 876 6 45 要求最后的输出格式: 1    5 2    6 3    8 4    45 5    45 5    876 首先,这个题目是须要对文 ...

  2. Highcharts中文教程

    http://www.hcharts.cn/docs/index.php?doc=start

  3. 经验19--C#大事

    以前学过C#大事.但我还没有搞懂怎么弄. 这一次,他们下进行了研究. 1.定义参数类,对于参数的传递活动.(可以省略) public class UserEventArgs     {         ...

  4. configure.ac:20: error: Autoconf version 2.65 or higher is required

    安装thrift例如,下面的问题出现: configure.ac:20: error: Autoconf version 2.65 or higher is required wget http:// ...

  5. 虚拟机VM10连衣裙Mac OS X 10.9.3

    最近WWDC终极大招释放--新的编程语言Swift(迅速),导致大波浪,渴望围观程序猿.当然,工欲善其事,其利润,因此,对于那些谁不Mac非常为难.可是,请放心.本文教你怎样在Windows下也能体验 ...

  6. XCL-Charts绘画面积图(AreaChart) 例1

    样本区域地图,发现区域图的时候把做向上注视位置图更具优势的管理.在改变. 区域图网格和轴是不一样的处理与其它图, 它是用来表示其影响范围的覆盖范围,车桥无段伸出. 在这里下处理. watermark/ ...

  7. IndexReader已解决的问题

    设计和实时搜索的发展,IndexReader饮酒数成为0当调用doClose,和SegmentReader再有一个addCoreClosedListener控制的方法SegmentCoreReader ...

  8. OpenGL中shader使用

    学了接近一个月的OpenGL,最终要排上用场了...好吧,就从学到的shader(着色器)开刀吧. 先简单的介绍shader,shader事实上是显卡的功能,就是利用显卡的GPU去做图像处理的工作,而 ...

  9. UI设计规范

    iphone\ipad.android UI设计规范对比 http://blog.163.com/leenell@yeah/blog/static/95840991201302210451710/ A ...

  10. Java IO的RandomAccessFile的使用(转)

    现有如下的一个需求,向已存在1G数据的txt文本里末尾追加一行文字,内容如下“Lucene是一款非常优秀的全文检索库”.可能大多数朋友会觉得这个需求很easy,说实话,确实easy,然后XXX君开始实 ...