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. 《Java并发编程实战》读书笔记-第4章 对象的组合

    设计线程安全的类 三个基本要素: 找出构成对象状态的所有变量 找出约束状态变量的不变性条件 建立对象状态的并发访问管理策略 实例封闭 将数据封装在对象内部,可以将数据的访问限制在对象的方法上,从而更容 ...

  2. java学习2-数据类型和运算符

    1.数据类型分类 java是强类型语言:a.所有的变量必须先声明后使用 b.指定类型的变量只能接受类型与之匹配的值 java语言支持的类型分为两类:基本类型和引用类型. 基本类型:包括boolean类 ...

  3. HashMap - 类注释

    了解到Java8以后HashMap的实现换了,也看了很多博客一直在向我这个小菜鸡说HashMap的重要.因此我决定洗心革面,好好正视HashMap 基于jdk 8 先从类注释开始入手学习,顺便提高提高 ...

  4. CentOS7 下升级Python版本

    来博客园的第一篇博客,以后要坚持养成记录.分享的习惯啊,这样生活才会有痕迹~ 服务器版本:CentOS 7.3 64位 旧Python版本:2.7.5 新Python版本:3.8.0 说明:本次配置使 ...

  5. Kafka集群的安装和部署

    一.Kafka的下载与解压 http://kafka.apache.org/downloads.html下载kafka_2.11-1.1.1.tgz.gz并解压到/home/jun下 [jun@mas ...

  6. stream流篇

    流是C#中比较重要的一个概念,很多关键技术都需要用到流.何为流呢?可以理解流为江河中水的流动,不过C#中则为信息流,我们可以把信息写入流,也可以读出.比如以文件读写操作为例,首先以某种方式(如只读)打 ...

  7. django-模板之模板变量(二)

    将views中的变量传递给html界面 book/views.py from django.views import View from django.shortcuts import render ...

  8. split分割文件与数据

    split主要用途:在之前计算机发展的时候,我们必须分割文件,才能将大量数据放入多张软盘中,而今我们分割文件有了其他的目的,比如提高可读性,生成日志以及发送有大小限制的E-mail附件. 工作原理:s ...

  9. win10系统格式化、恢复出厂设置的操作步骤

    恢复电脑出厂设置具体步骤

  10. fastfdfs上传代码

    //上传到FDFS //获取后缀名 String extension = StringUtils.substringAfterLast(file.getOriginalFilename()," ...