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 学习笔记(19) Control Invocation

    [Cocos2d-x 学习笔记 目录链接] 1. 简介 control为其子类提供了touch回调函数,当子类触发EventType相关事件时,会调用相关的回调函数. control对象接收到的事件类 ...

  2. Halcon一日一练:图像采集设备的基本参数

    因操作图像处理之前,需要对图像进行采集.采集图像,我们首先要确定的是图像的像素和采集的效率.这些都需要对设备进行配置与操作.现实情况是图像设备有各自不同的采集方式,配置也各不相同.这就需要设备提供商提 ...

  3. springMVC初学简单例子

    新建web项目,保留web.xml. 配置web.xml文件(/WEB-INF/下): <?xml version="1.0" encoding="UTF-8&qu ...

  4. 高性能封装检测浏览器支持css3属性函数

    css3出来已经很久了,现在来谈判断浏览器是否支持某个css3的属性虽说有点过时了,但是还是可以谈谈的,然后,此篇主要谈的不是判断是否支持,而是怎么封装更好,为什么这么封装,欢迎吐槽. 入题,判断浏览 ...

  5. SpringBoot中如何优雅的读取yml配置文件?

    YAML是一种简洁的非标记语言,以数据为中心,使用空白.缩进.分行组织数据,从而使得表示更加简洁易读.本文介绍下YAML的语法和SpringBoot读取该类型配置文件的过程. 本文目录 一.YAML基 ...

  6. js小数加减乘除时精度不准确

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/qq_33237207/article/d ...

  7. Unity 单例模式

    明天十一放假,今天不知什么原因看到一篇unity单例模式的介绍,瞬间来了戾气. (一)最简单的单利 public class WebRequestUtility : MonoBehaviour { p ...

  8. 你编写的Java代码是咋跑起来的?

    如果你是一名 Java 开发人员,你肯定指定 Java 代码有很多种不同的运行方式.比如说可以在开发工具(IDEA.Eclipse等)中运行,可以双击执行 jar 文件运行,也可以在命令行中运行,甚至 ...

  9. 设计模式C++描述----05.职责链(Chain of Responsibility)模式

    一. 概述 职责链模式: 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止. 二. 举个例子 员工要求加薪 ...

  10. nginx基于uwsgi部署Django

    1.安装nginx yum install -y nginx(需要epel源) 2.安装uwsgi yum groupinstall "Development tools" yum ...