Same binary weight

时间限制:300 ms  |  内存限制:65535 KB
难度:3
 
描述

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
解题思路 1. 将右起第一个“01”,改变为“10” 2. 将该“01”后面的所有“1”移动至最后
#include <iostream>
#include <string>
#include <bitset>
#include <algorithm>
using namespace std; int main(){
int n;
while(cin >> n){
bitset<> bitInt(n);
string strInt =bitInt.to_string();
int pos = strInt.rfind("");
swap(strInt[pos],strInt[pos+]);
if(pos+ < ) sort(strInt.begin()+pos+,strInt.end());
cout<<bitset<>(strInt).to_ulong()<<endl;
}
}

string rfind("01")从字符串右边找”01“然后返回”01“的位置

将01后面的1移动到最后相当于对01后面的字符串排序



ACM Same binary weight的更多相关文章

  1. Same binary weight (位运算)

    题目描述 The binary weight of a positive  integer is the number of 1's in its binary representation.for ...

  2. nyoj 412 Same binary weight ()

    Same binary weight 时间限制:300 ms  |  内存限制:65535 KB 难度:3   描述 The binary weight of a positive  integer ...

  3. nyoj 412-Same binary weight (bitset ,to_ulong())

    412-Same binary weight 内存限制:64MB 时间限制:0ms 特判: No 通过数:2 提交数:3 难度:3 题目描述: The binary weight of a posit ...

  4. 【ACM】Binary String Matching

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

  5. [转]XNOR-Net ImageNet Classification Using Binary Convolutional Neural Networks

    感谢: XNOR-Net ImageNet Classification Using Binary Convolutional Neural Networks XNOR-Net ImageNet Cl ...

  6. 论文翻译:Ternary Weight Networks

    目录 Abstract 1 Introduction 1.1 Binary weight networks and model compression 2 Ternary weight network ...

  7. caffeModels--models-caffes-大全

    caffe的伯克利主页:http://caffe.berkeleyvision.org/caffe的github主页:https://github.com/BVLC/caffe caffe的model ...

  8. nyoj412_bitset_

    Same binary weight 时间限制:300 ms  |  内存限制:65535 KB 难度:3   描述 The binary weight of a positive  integer ...

  9. [综述]Deep Compression/Acceleration深度压缩/加速/量化

    Survey Recent Advances in Efficient Computation of Deep Convolutional Neural Networks, [arxiv '18] A ...

随机推荐

  1. DLog的使用

    DLog本质上就是个宏替换.DLog具体代码如下: #ifdef DEBUG #define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt) ...

  2. php 会话控制

    会话控制 HTTP协议,在TCP协议基础上的HTTP协议称为无状态协议 SESSION COOKIE SESSION特点:1.存储在服务器.2.每个使用者都会生成一个SESSION.3.有默认的过期时 ...

  3. tar 打包文件 除某个文件夹

    tar -cvf test2.tar --exclude=test/test10 test/

  4. JDBC 精度

    http://www.cnblogs.com/tobecrazy/p/3390021.html http://www.cnblogs.com/kerrycode/p/4034231.html http ...

  5. WPF框架MVVM简单例子

    MVVM是Model-View-ViewModel的缩写形式,它通常被用于WPF或Silverlight开发.Model——可以理解为带有字段,属性的类.View——可以理解为我们所看到的UI.Vie ...

  6. Generic Access Profile

    转自:https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile ​​Assigned numbe ...

  7. 在MAVEN仓库中添加ORACLE JDBC驱动

    本文转载自 http://www.cnblogs.com/leiOOlei/archive/2013/10/21/3380568.html 因为已经是第二次遇到,所以COPY过来,怕以后别人的BLOG ...

  8. 常见IE浏览器bug及其修复方案(双外边距、3像素偏移、绝对定位)

    1. 双外边距浮动bug IE6和更低版本中存在双外边距浮动bug,顾名思义,这个Windows bug使任何浮动元素上的外边距加倍 bug重现: <!DOCTYPE html> < ...

  9. 类模板Queue的实现

    #include <iostream> #include <vector> using namespace std; template <class Type> c ...

  10. loadrunner写入数据到文件

    首先在F盘创建: F:\\LR\\test.txt文件 #define USERCOUNT 100    //定义参数数量 Action() {     char *namecommon=" ...