Same binary weight

时间限制:300ms  |  内存限制:65535KB
难度: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

题解:先把数字转化为字符数组。然后通过next_permutation函数生成下一个字符数组,然后在转化为数字。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <list>
#define LL long long
#define INF 0x7fffffff
#define FIN 0x80000000
using namespace std;
char str[50];
void handle(int x)
{int k=31;while(x){if(x&0x1) str[k--]='1';else str[k--]='0';x>>=1;}for(int i=0;i<=k;i++) str[i]='0';
}
int main()
{int n;while(scanf("%d",&n)!=EOF){handle(n);next_permutation(str,str+32);int m=0;for(int i=0;i<32;i++)m=m*2+str[i]-'0';printf("%d\n",m);}
}

NYOJ 412 Same binary weight题解相关推荐

  1. Same binary weight (位运算)

    题目描述 The binary weight of a positive  integer is the number of 1's in its binary representation.for ...

  2. 二值网络--Training Binary Weight Networks via Semi-Binary Decomposition

    Training Binary Weight Networks via Semi-Binary Decomposition ECCV2018 CNN模型的压缩或加速总体上分为三类: pruning-b ...

  3. nyoj 题目5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alpha ...

  4. [KSN2021] Binary Land题解(位运算应用)

    P7973 [KSN2021] Binary Land [KSN2021] Binary Land 题目描述 给定一张 N N N 个点的图,每个点有权值 A i A_i Ai​ 和价值 B i B_ ...

  5. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  6. 2019牛客全国多校训练三 题解

    A Gragh Games Unsolved. B Crazy Binary String 题解:水题,子序列只要统计0和1数量,取最小值然后乘2就是答案: 对于子串:先记录0和1 前缀和的差值,然后 ...

  7. 由任意二叉树的前序遍历序列和中序遍历序列求二叉树的思想方法_算法与数据结构基础 - 二叉树(Binary Tree)...

    二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...

  8. 算法与数据结构基础 - 堆(Heap)和优先级队列(Priority Queue)

    堆基础 堆(Heap)是具有这样性质的数据结构:1/完全二叉树 2/所有节点的值大于等于(或小于等于)子节点的值: 图片来源:这里 堆可以用数组存储,插入.删除会触发节点shift_down.shif ...

  9. ECCV 2018 完整论文集 -- List 下载链接

    下文列表为ECCV2018官网得到了今年接收论文列表,共779篇: 下文为ECCV2018的全部接收论文汇总 Oral: Convolutional Networks with Adaptive Co ...

  10. r语言c函数怎么用,R语言学习笔记——C#中如何使用R语言setwd()函数

    在R语言编译器中,设置当前工作文件夹可以用setwd()函数. > setwd("e://桌面//") > setwd("e:\桌面\") > ...

最新文章

  1. 科学家王海峰:从百度十篇论文入选ACL 2019说起
  2. Hinton口中破解宇宙终极秘密的GPT-3厉害在哪?这有篇涂鸦详解
  3. 【crontab】误删crontab及其恢复
  4. Vijos P1103 校门外的树【线段树,模拟】
  5. iOS 判断纯汉字,还是是否含有汉字
  6. 【转】深入理解Windows消息机制
  7. 利用自定义web-font实现数据防采集
  8. 44年前的今天,改变世界的TA诞生了! | 极客头条
  9. python中input数组_python – 在NumPy数组中搜索序列
  10. Win-MASM64汇编语言-$/取当前行代码的地址
  11. 服务企业互联网化 打造社会化商业平台
  12. chrome浏览器安装infinity插件
  13. Android免打包多渠道统计如何实现?通用流行框架大全
  14. 成长性思维和富人思维
  15. 08-cmd定时关机和取消定时关机
  16. dubbo优点是什么dubbo有哪些缺点
  17. 对比顺序表与链表——纵观与取舍
  18. 知识付费直播使用https方法 教程
  19. VM2230 asdebug.js:xxx:端口不在以下 request 合法域名列表中
  20. 思科 | VLAN 间路由实验(三层交换机)

热门文章

  1. java poi 自动行高_POI设置单元格自动行高(思路)
  2. 从逻辑功能上看 可以把计算机网络分成,从逻辑功能上看,可以把计算机网络分成通信子网和资源子网。...
  3. 安卓机被锁屏的6种解锁方法
  4. android 锁屏应用,推荐几款好用的安卓(Android)手机锁屏软件
  5. Linux环境下安装Tableau Server
  6. 最全的搜索引擎入口和分类目录入口
  7. layer子父传值(1)
  8. 汇总报表与合并报表之如何汇总报表
  9. elasticsearch 文档增删改
  10. WPS中用mathtype插入公式的方法