题目描述

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. 设单链表中存放n个字符,试设计一个算法,使用栈推断该字符串是否中心对称

    转载请注明出处:http://blog.csdn.net/u012860063 问题:设单链表中存放n个字符.试设计一个算法,使用栈推断该字符串是否中心对称,如xyzzyx即为中心对称字符串. 代码例 ...

  2. 【iOS】文件上传小记

    iOS由该系统提供API可以实现可以实现文件的上传和下载,有两种方法来. NSURLConnection与NSURLSession. 当中NSURLConnection是使用非常久的的一种方式.NSU ...

  3. Vc6.0打开该文件坠毁

    这是一个微软bug,下面是用户给出的溶液: http://blog.163.com/wjatnx@yeah/blog/static/12758622820138110530322/ http://bl ...

  4. Python日志输出格式和时间格式

    formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s","%Y%b%d-%H:%M:% ...

  5. Intel® Ethernet Connection I217-V 网卡驱动(win10 ,2012)

    https://downloadcenter.intel.com/zh-cn/download/25016/-Windows-10 上面是win10 的驱动 win8.1  https://downl ...

  6. 【SQL Server性能优化】SQL Server 2008该表压缩

    当数据库是比较大的,而当你想备份,我们可以启动数据库备份压缩.这项由于备份文件比较小的压缩,所以整个备份的更快的速度,同时还低了磁盘空间的消耗. 当然还有一方面.肯定会添加cpu的消耗.只是一般的se ...

  7. shell编程三大神器之grep

  8. Hadoop Mapreduce刮

    前言 的一个渣渣程序猿一枚,因为个人工作,须要常常和hadoop打交道,可是自己之前没有接触过hadoop.所以算是边学边用,这个博客算是记录一下学习历程,梳理一下自己的思路,请各位看官轻拍.本博客大 ...

  9. Android 无法Bind Service

    今天帮同学看一个bindService的样例,说是无法bind service(他接触android时间不长),那代码是从网上找的一个样例,结果看了半天,才终于找到问题所在了),这个问题真是令人挺无语 ...

  10. Android_Training

    http://wiki.eoeandroid.com/Android_Training Android小白成长之0基础篇