nyoj 412-Same binary weight (bitset ,to_ulong())
2024-10-01 03:59:31
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());
}
}
最新文章
- Java豆瓣电影爬虫——小爬虫成长记(附源码)
- Mybatis框架基于映射文件和配置文件的方式,实现增删改查,可以打印日志信息
- 类似网易新闻 title栏 滚动时 文字放大&;变色
- 【java基础学习】GUI
- shell总结(0基础入门)
- cf 547B. Mike and Feet dp
- MeasureSpec介绍
- 游戏文字自动断行需要,还得从 UTF-8 讲起
- ios调打电话代码
- php 带cookie采集某页面
- SQL 查询某字段id为空(不为空)
- 微信OPENID授权方法
- 原生javascript 制作canvas 验证码
- 初始配置JDK
- HTTPS加密原理
- Gcd&;Exgcd算法学习小记
- 新闻娱乐类APP的后端核心逻辑总结
- 【算法随记】Canny边缘检测算法实现和优化分析。
- iOS高德地图SDK定位和搜索附近信息的具体使用
- 若父设置了overflow: hidden;子如何不受影响
热门文章
- JavaScript回调函数和递归函数
- 坚果云Markdown - 文档管理编辑器
- Etcd安装和使用
- 史上最轻松入门之Spring Batch - 轻量级批处理框架实践
- 使用zrender.js绘制体温单(2)
- 微信小程序中的canvas基础应用
- vue+webpack+element-ui项目打包优化速度与app.js、vendor.js打包后文件过大
- Prism - MVVM模式下,StackPanel中增加和删除View(UserControl)
- CSS盒模型以及如何解决边距重叠问题
- 从一个简单的例子看spring ApplicationContext上下文隔离