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. C# 关于config文件中的usersettings

    在调整app.config的时候遇到了一点问题,把这个问题记录下来,可能我只是没有找到解决方案,问题本身也许并不复杂. 在VS中通过Properties中的Settings.settings来设置作用 ...

  2. Jackson替换fastjson

    为什么要替换fastjson 工程里大量使用了fastjson作为序列化和反序列化框架,甚至ORM在处理部分字段也依赖fastjson进行序列化和反序列化.那么作为大量使用的基础框架,为什么还要进行替 ...

  3. Dell R720 RAID配置

    Dell服务器上一般都带有Raid卡,Raid5配置请看下边,亲们 1. 将服务器接上电源,显示器,键盘,并开机 2. 按 ctrl + R进入Raid设置 3. 将光标放置在Raid卡那,按F2,选 ...

  4. [案例分析] 政务云市场面临的复杂格局——重庆政务云模式的启示:多厂商竞争化、PaaS 化

    新闻背景: 2019 年 9 月底,重庆市大数据应用发展管理局发布政务云平台采购公告,预算金额为 5000 万元,以上 4 家入选. 最终项目被项目被阿里云.腾讯云.华为云.紫光云 4 家瓜分. 50 ...

  5. 谢宝友: 手把手教你给Linux内核发patch

    本文系转载,著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者: 谢宝友 来源: 微信公众号 linux阅码场 (id: linuxdev) 本文简介       本文一步一 ...

  6. Leetcode Tags(3)String(TODO)

    一.Easy 696 Count Binary Substrings Input: "00110011" Output: 6 Explanation: There are 6 su ...

  7. Mysql数据库(四)表记录的更新操作

    一.插入表记录 1.使用INSERT...VALUES语句插入新纪录 (1)插入完整数据 mysql> desc tb_manager; +-------+------------------+ ...

  8. Unity混合天空盒

    对于不同天气下天空盒的实现. 天空盒时通过天空盒材质更改实现的,新建材质,选择shader/skybox/6sided,然后添加六个天空盒贴图就可以实现天空盒,如果想实现天气变化则需要至少两套贴图,并 ...

  9. jupyter qtconsole 的安装

    Jupyter qtconsole最近开始研究人工智能算法,发现了一款基于python的科学计算的神器,jupyter qtconsole,简直就是ipython的加强版,每个命令都直接显示帮助信息, ...

  10. 汇编语言——物理地址=段地址x16+偏移地址,检测点2.2

    一.为什么 物理地址=段地址x16+偏移地址? 刚开始学时,我都笨到不明白为什么是2的N次方,咱把物理地址就当数字,计算机中数字是由很多位0或1自由组合的, 而每一位上要么是0要么是1,只有这两种情况 ...