nyoj 412 Same binary weight ()
Same binary weight
- 描述
-
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 ()的更多相关文章
- NYOJ 5 字符串处理 find()函数应用
http://acm.nyist.net/JudgeOnline/problem.php?pid=5 #include<stdio.h> #include<iostream> ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- 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 ...
- 二叉搜索树(Binary Search Tree)
二叉搜索树(BST,Binary Search Tree),也称二叉排序树或二叉查找树. 二叉搜索树:一棵二叉树,可以为空:如果不为空,满足以下性质: 非空左子树的所有键值小于其根结点的键值: 非空右 ...
- 折半插入排序(Binary Insertion Sort)的C语言实现
原创文章,转载请注明来自钢铁侠Mac博客http://www.cnblogs.com/gangtiexia 折半插入排序(Binary Insertion Sort)的基本思想是将新记录插入到已经 ...
- 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 ...
- 二叉查找树(Binary Sort Tree)(转)
二叉查找树(Binary Sort Tree) 我们之前所学到的列表,栈等都是一种线性的数据结构,今天我们将学习计算机中经常用到的一种非线性的数据结构--树(Tree),由于其存储的所有元素之间具有明 ...
- LeetCode算法题-Construct String from Binary Tree(Java实现)
这是悦乐书的第273次更新,第288篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第141题(顺位题号是606).构造一个字符串,该字符串由二叉树中的括号和整数组成,并具 ...
- 【CF662C】Binary Table(FWT)
[CF662C]Binary Table(FWT) 题面 洛谷 CF 翻译: 有一个\(n*m\)的表格(\(n<=20,m<=10^5\)), 每个表格里面有一个\(0/1\), 每次可 ...
随机推荐
- Echart:前端很好的数据图表展现工具+demo
官网: http://echarts.baidu.com/index.html 通过一个简单的小Demo介绍echart的使用:demo均亲测可以运行 demo1: 1.新建一个echarts.ht ...
- PHP array_unshift
1.函数的作用:在数组的开头插入一个或者多个元素 2.函数的参数: @params array &$array @params mixed $value1 @params mixed ...
- kafka JavaAPI遇到的坑
症状:Producer连不上,提示没有可用Node. 解决:在安装kafka的目录中配置server.properties 1.listeners=PLAINTEXT://:9092或listener ...
- vue-router之to属性赋值
to属性赋值 <!-- html --> <div id="app"> <router-link to="/bj/朝阳区"> ...
- 【Spring Cloud】服务注册与发现组件——Eureka(二)
一.Eureka原理 1.架构图 首先来看eureka的官方结构图 所有应用作为Eureka Client和Eureka Server交互,服务提供者启动时向Eureka Server注册自己的IP. ...
- java架构之路-(Redis专题)简单聊聊redis分布式锁
这次我们来简单说说分布式锁,我记得过去我也过一篇JMM的内存一致性算法,就是说拿到锁的可以继续操作,没拿到的自旋等待. 思路与场景 我们在Zookeeper中提到过分布式锁,这里我们先用redis实现 ...
- 百万年薪python之路 -- 列表
1.列表(list)-- list关键字 列表是python的基础数据类型之一,有顺序,可以切片方便取值,它是以[ ]括起来, 每个元素用' , '隔开而且可以存放各种数据类型(字符串,数字,布尔值, ...
- linux 基本操作--笔记
linux 基本操作: pwd 显示当前目录 ll 用于查看文件和目录,即list,其参数比较多 -l 列出数据串,包含文件的属性和权限数据等 -a 列出全部文件,包含隐藏文件 -d 仅列出目录本身, ...
- GO基础之闭包
一.闭包的理解 闭包是匿名函数与匿名函数所引用环境的组合.匿名函数有动态创建的特性,该特性使得匿名函数不用通过参数传递的方式,就可以直接引用外部的变量. 这就类似于常规函数直接使用全局变量一样,个人理 ...
- CountDownLatch和CycliBarrier介绍
一.CountDownLatch 它被用来同步一个或多个任务,强制他们等待其他任务完成,这就是闭锁. public CountDownLatch(int count) { if (count < ...