ACM Same binary weight
2014-04-19 16:39
288 查看
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<32> bitInt(n); string strInt =bitInt.to_string(); int pos = strInt.rfind("01"); swap(strInt[pos],strInt[pos+1]); if(pos+2 < 31) sort(strInt.begin()+pos+2,strInt.end()); cout<<bitset<32>(strInt).to_ulong()<<endl; } }
string rfind("01")从字符串右边找”01“然后返回”01“的位置
将01后面的1移动到最后相当于对01后面的字符串排序
相关文章推荐
- SP2010开发和VS2010专家"食谱"--第一章节--列表和事件接收器(7)--部署事件接收器
- 论存储IOPS和Throughput吞吐量之间的关系
- sqlserver2005 自动备份的维护计划图解教程
- 为什么兴趣图谱将重塑我们的社交网络及未来的商业
- Android Intent 的几种启动活动的方式
- Debian7.3 x86_64位系统无法启动adb如何解决
- opencv_协方差矩阵与协方差讲解
- region split流程分析
- Python第一步
- Java知识总结——对象创建过程
- 常用linux小工具介绍
- opencv_协方差矩阵与协方差讲解
- android之自定义ViewGroup和自动换行的布局的实现
- 02_Linux学习_命令
- 我们是这样使用腾讯风铃的!
- 目标跟踪学习笔记_4(particle filter初探3)
- Intent.setClass(this,className),第一个参数
- Redis配置文件参数说明
- 再次签到
- pl/sql 本机不安装oracle服务端连接服务器