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的个数与所给的数的二进制中1的个数相同
算法1:
①、算出的二进制数从左到右遍历
②、if 第i位数为1 && 第i + 1 位数位0:
swap (A[i], A[i + 1])
pos = i
break
③、将第0位到第pos位的所有1移动到最右边
**/

C/C++代码实现(算法1):

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <stack>
#include <queue>
#include <map> using namespace std; int n; int main () {
while (~scanf ("%d", &n)) {
int A [] = {}, ans = , m = n, pos, k = , cnt = ; while (m) {
A [k ++] = m%;
m /= ;
}
for (int i = ; i < k; ++ i) {
if (A [i] && !A[i + ]) {
swap (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] = ;
}
}
for (int i = ; i <= k; ++ i) {
ans += pow (, i) * A[i];
}
printf ("%d\n", ans);
}
}

算法2:

/**
  区别于算法一的是计算二进制的算法 ==> bitset <32> A (n) (说明:将n转换为二进制储存在A中)
二进制转换为十进制 ==> A.to_ulong()
**/

C/C++实现算法2:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <stack>
#include <queue>
#include <map>
#include <bitset> using namespace std; int n; int main () {
while (~scanf ("%d", &n)) {
bitset <> A (n); // 将n转化为2进制储存在A中
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()); // 将得到的二进制转化为无符号十进制
}
}

nyoj 412 Same binary weight ()的更多相关文章

  1. NYOJ 5 字符串处理 find()函数应用

    http://acm.nyist.net/JudgeOnline/problem.php?pid=5 #include<stdio.h> #include<iostream> ...

  2. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  3. 2016年11月3日JS脚本简介数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6.布尔型数据:bool 7.对象类型:object 8.二进制:binary 语言类型: 1.强类型语言:c++ c c# java 2.弱类型语

    数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6 ...

  4. 二叉搜索树(Binary Search Tree)

    二叉搜索树(BST,Binary Search Tree),也称二叉排序树或二叉查找树. 二叉搜索树:一棵二叉树,可以为空:如果不为空,满足以下性质: 非空左子树的所有键值小于其根结点的键值: 非空右 ...

  5. 折半插入排序(Binary Insertion Sort)的C语言实现

    原创文章,转载请注明来自钢铁侠Mac博客http://www.cnblogs.com/gangtiexia   折半插入排序(Binary Insertion Sort)的基本思想是将新记录插入到已经 ...

  6. LeetCode 606. Construct String from Binary Tree (建立一个二叉树的string)

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  7. 二叉查找树(Binary Sort Tree)(转)

    二叉查找树(Binary Sort Tree) 我们之前所学到的列表,栈等都是一种线性的数据结构,今天我们将学习计算机中经常用到的一种非线性的数据结构--树(Tree),由于其存储的所有元素之间具有明 ...

  8. LeetCode算法题-Construct String from Binary Tree(Java实现)

    这是悦乐书的第273次更新,第288篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第141题(顺位题号是606).构造一个字符串,该字符串由二叉树中的括号和整数组成,并具 ...

  9. 【CF662C】Binary Table(FWT)

    [CF662C]Binary Table(FWT) 题面 洛谷 CF 翻译: 有一个\(n*m\)的表格(\(n<=20,m<=10^5\)), 每个表格里面有一个\(0/1\), 每次可 ...

随机推荐

  1. ‎Cocos2d-x 学习笔记(20) ControlButton

    [Cocos2d-x 学习笔记 目录链接] 1. 简介 ControlButton实现了按钮功能,根据触摸的位置和移动的过程可识别9中EventType类型,执行对应的回调函数. 直接继承了Contr ...

  2. RAW网络编程

    LWIP提供了三种的可以被应用程序直接调用的接口API: (1)       低水平的,基于内核/回调函数的API(后面称 RAW API)    适用于数据量不大,没有os的MCU (2)      ...

  3. css的简单使用

    css语法 id选择器: id 选择器可以为标有特定 id 的 HTML 元素指定特定的样式. HTML元素以id属性来设置id选择器,CSS 中 id 选择器以 "#" 来定义. ...

  4. Fiddler抓包和工作原理

    一.概述 Fiddler是一款免费且功能强大的数据包抓取软件.它通过代理的方式获取程序http通讯的数据, 可以用其检测网页和服务器的交互情况,能够记录所有客户端和服务器间的http请求, 支持监视. ...

  5. Intellij idea 自动生成serialVersionUID

    1 什么是UID 网络间的数据传输最终都是要转化为二进制流的方式进行传输,为了方便转换以及进行验证,我们应该把对角序列化,当实现Seriabizable接口时,UID就是一个必须的属性,可以方便进行版 ...

  6. 基于STM32F103和Cube的输入捕获例程

    1.开发环境 (1)Cube5.24 (2)Keil5 (3)STM32F103 2.Cube配置 Cube配置很简单,只要打开TIM4通道1的引脚,设置为输入捕获模式,在配置是高或低电平沿触发 TI ...

  7. SoapUI 关联之Property Transfer、JSONPath、Xpath

    进行接口功能测试过程中,经常会碰到,需要获取到上一个请求响应结果中数据,传递到下一个请求中来使用.在soapui中我们通过Property Transfer来实现. 1.Property Transf ...

  8. codeblocks 调试不停止的解决办法。

    CB的工程路径不能有中文,也不能有空格. 所以一定要全英文路径,而且空格要用下划线代替. 否则,调试的时候,codeblocks不会在断点处停止.

  9. 150行代码搭建异步非阻塞Web框架

    最近看Tornado源码给了我不少启发,心血来潮决定自己试着只用python标准库来实现一个异步非阻塞web框架.花了点时间感觉还可以,一百多行的代码已经可以撑起一个极简框架了. 一.准备工作 需要的 ...

  10. UITabView

    UITabView可是实现列表功能,此文转自https://www.cnblogs.com/longiang7510/p/5367080.html,讲述很详细,都有注视,但是注释解释不太确切,可以自行 ...