题目描述

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. LeanCloud获取最近会话列表和获取最后一条聊天记录

    最近公司有项目需要集成IM聊天功能,领导要求用LeanCloud集成,搞不出来就要背包滚蛋啊,没办法只能硬着头皮搞了. 刚拿到官方提供的demo感觉:嗯,不错.图片语音啥的都有了,但尼玛这还不够啊,还 ...

  2. 希尔排序----java实现

    思路:希尔排序是分组基础上的直接插入排序,给定的一个步长数组,每个小组先直接插入排序.虽然有四次循环,但是每次循环次数少. package com.sheepmu.text; import java. ...

  3. Android - 用Fragments实现动态UI - 和其他Fragments通信

    为了重用Fragment UI组件,应该把每个都设计为它自有的模块组件并且有自己的布局和行为.一旦定义了这些可重用的Fragment,你可以把它们和一个activity关联然后和程序的逻辑一起实现上层 ...

  4. Akka.NET是Java/Scala 流行框架Akka的一个 .NET 开源移植

    Akka.NET v1.0 已发布,支持Mono Akka.NET 是Java/Scala 流行框架Akka的一个 .NET 开源移植.可用于构建高并发,分布式和容错事件驱动的应用在 .NET 和 M ...

  5. scrot-0.8

    相关库下载地址:         www.sunfreeware.com/programlistsparc10.html tar -zxvf scrot-0.8.tar.gzcd scrot-0.8. ...

  6. crm2011i创建nt类型字段

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Xrm.Sdk.Messages;     using Microsoft ...

  7. LeetCode: Palindrome Partitioning [131]

    [称号] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  8. EF6操作Sqlite数据库的项目兼容性问题

    vs2010无法正确打开2015创建的项目里面操作Sqlite数据库时使用EF6创建的edmx文件(会显示空白)   但是可以正常查询 vs2015无法正确打开2010创建的项目里面操作Sqlite数 ...

  9. 百度地图 iOS SDK - 坐标转换方法

    百度地图 Android SDK 要么 iOS SDK 或各种 API 工具产品,我们使用百度自己的加密坐标系. 员在使用过程中,位置点都是通过 GPS 或者其它途径获取的.所以与百度地图所使用的坐标 ...

  10. NYoj-Binary String Matching-KMP算法

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 Given two strings A and B, whose alp ...