412-Same binary weight

内存限制:64MB
时间限制:0ms
特判: No

通过数:2
提交数:3
难度: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

C/C++  AC:

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <climits>
#include <bitset>
#define PI 3.1415926 using namespace std;
const int MY_MAX = ;
int N, M; int main()
{
while (cin >>N)
{
bitset <> A(N);
int pos = , cnt = ;
for (int i = ; i <= ; ++ i)
{
if (A[i] && !A[i + ])
{
A[i] = , A[i + ] = ;
pos = i;
break;
}
if (A[i])
cnt ++;
} for (int i = ; i < pos; ++ i)
{
if (cnt)
{
A[i] = ;
cnt --;
}
else
A[i] = ;
}
printf("%d\n", A.to_ulong());
}
}

最新文章

  1. Java豆瓣电影爬虫——小爬虫成长记(附源码)
  2. Mybatis框架基于映射文件和配置文件的方式,实现增删改查,可以打印日志信息
  3. 类似网易新闻 title栏 滚动时 文字放大&amp;变色
  4. 【java基础学习】GUI
  5. shell总结(0基础入门)
  6. cf 547B. Mike and Feet dp
  7. MeasureSpec介绍
  8. 游戏文字自动断行需要,还得从 UTF-8 讲起
  9. ios调打电话代码
  10. php 带cookie采集某页面
  11. SQL 查询某字段id为空(不为空)
  12. 微信OPENID授权方法
  13. 原生javascript 制作canvas 验证码
  14. 初始配置JDK
  15. HTTPS加密原理
  16. Gcd&amp;Exgcd算法学习小记
  17. 新闻娱乐类APP的后端核心逻辑总结
  18. 【算法随记】Canny边缘检测算法实现和优化分析。
  19. iOS高德地图SDK定位和搜索附近信息的具体使用
  20. 若父设置了overflow: hidden;子如何不受影响

热门文章

  1. JavaScript回调函数和递归函数
  2. 坚果云Markdown - 文档管理编辑器
  3. Etcd安装和使用
  4. 史上最轻松入门之Spring Batch - 轻量级批处理框架实践
  5. 使用zrender.js绘制体温单(2)
  6. 微信小程序中的canvas基础应用
  7. vue+webpack+element-ui项目打包优化速度与app.js、vendor.js打包后文件过大
  8. Prism - MVVM模式下,StackPanel中增加和删除View(UserControl)
  9. CSS盒模型以及如何解决边距重叠问题
  10. 从一个简单的例子看spring ApplicationContext上下文隔离