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. Oracle RAC 连接

    http://blog.csdn.net/hijk139/article/details/7452553 http://blog.itpub.net/4227/viewspace-677272/ ht ...

  2. ubuntu 下使用 putty 调试

    转自:http://blog.csdn.net/wh_19910525/article/details/39313457 Ubuntu的机子上,插上USB2COM线,准备开工. 检查USB2COM在本 ...

  3. probe函数何时调用的

    转自:http://blog.csdn.net/xiafeng1113/article/details/8030248 Linux中 probe函数何时调用的 所以的驱动教程上都说:只有设备和驱动的名 ...

  4. SQL Server创建随机测试数据

    我们在做数据仓库开发的过程中,经常需要插入大量的测试数据来测试数据库查询性能和计算占用的存储空间等.本文主要介绍下不借用第三方的工具在数据库中直接生成大量的测试数据. 需求 每一行包含5个日期字段和一 ...

  5. html5 离线存储 worker

    html5 离线存储 <!DOCTYPE html> <html manifest="cache.manifest"> <!--manifest存储- ...

  6. AgileEAS.NET SOA 中间件2013第四季度发布&部分功能开源预告

    一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...

  7. [JavaCore]JAVA中的泛型

    JAVA中的泛型 [更新总结] 泛型就是定义在类里面的一个类型,这个类型在编写类的时候是不确定的,而在初始化对象时,必须确定该类型:这个类型可以在一个在里定义多个:在一旦使用某种类型,在类方法中,那么 ...

  8. linux的<pthread.h>

    转自:http://blog.sina.com.cn/s/blog_66cc44d00100in5b.html Linux系统下的多线程遵循POSIX线程接口,称为pthread.编写Linux下的多 ...

  9. GitHub 使用教程图文详解(转)

    大纲: 一.前言 二.GitHub简介 三.注册GitHub账号 四.配置GitHub 五.使用GitHub 六.参与GitHub中其它开源项目 七.总结 注,GitHub官网:https://git ...

  10. 智能车学习(四)—— Cmp学习

    一.代码共享 1.cmp.h #ifndef HSCMP_H #define HSCMP_H //1 头文件 #include "common.h" //2 宏定义 //2.1比较 ...