An Easy Problem
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8333   Accepted: 4986

Description

As we known, data stored in the computers is in binary form. The problem we discuss now is about the positive integers and its binary form.

Given a positive integer I, you task is to find out an integer J, which is the minimum integer greater than I, and the number of '1's in whose binary form is the same as that in the binary form of I.

For example, if "78" is given, we can write out its binary form, "1001110". This binary form has 4 '1's. The minimum integer, which is greater than "1001110" and also contains 4 '1's, is "1010011", i.e. "83", so you should output "83".

Input

One integer per line, which is I (1 <= I <= 1000000).

A line containing a number "0" terminates input, and this line need not be processed.

Output

One integer per line, which is J.

Sample Input

1
2
3
4
78
0

Sample Output

2
4
5
8
83

Source

【思路】:会发现 78 :1001110  
                        83:1010011
  83的二进制是78二进制从右往做扫扫到01,将这个1左移一位,剩下的1右移
网上有种方法一行代码的大神 渣渣我直接看不懂 
这是哪门子贪心?orz  bb我要狗带了
【代码】
 //    Presentation Error(展示错误orz)    mlgb没换行符报错 第一次出现这种错误整个人都蒙圈了
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int er[];
int main()
{
int n;
while(cin>>n&& n)//当能输出且不为零时
{
int k=,tot=;
memset(er,,sizeof(er));
while(n)//计算二进制
{
er[++k]=n%;
n/=;
}
k++;
for(int i=;i<=k;i++)
{
if(er[i]==)//找1的个数
{
tot++;
er[i]=;
if(er[i+]==)//从低位到高位找01
{
er[i+]=;//找到了就改为1; 相当于左移了
break;
}
}
}
for(int i=;i<=tot-;i++)
{
er[i]=;//将tot-1个1放在末尾,(右移),tot-1是因为其中一个左移了
}
int sum=;
for(int i=k;i>=;i--)//将二进制转换为十进制输出
{
sum=sum*+er[i];
}
printf("%d\n",sum);
}
return ;
}

最新文章

  1. nuget常用命令
  2. 撸一段 SQL ? 还是撸一段代码?
  3. checkbox的全选与反选
  4. jws.mono脚本安装详解
  5. Mybatis 缓存
  6. 32位计时器极端情况下产生的bug
  7. Python一行代码
  8. jQuery练习一好友列表变色
  9. 如何在Ubuntu下使用TF/SD 卡制作Exynos 4412 u-boot启动盘
  10. 理解JavaScript原型式继承
  11. 解决Cacti监控图像断断续续问题
  12. python 基础篇(二)数据类型概述
  13. mac的svn之cornerstone简易教程
  14. CodeVS 3415-最小和
  15. JS计算字符串长度(中文算2个)
  16. spring配置文件详解【总结】
  17. 关于UITableView的一些老是忘记的小杂碎
  18. tomcat 拒绝服务
  19. private static final long serialVersionUID = 1L这句话到底什么意思?
  20. Guava学习笔记(一):Maven

热门文章

  1. iOS 52个技巧学习心得笔记 第二章 对象 , 消息, 运行期
  2. 每天一个Linux命令(40)vmstat命令
  3. 01_Hadoop学习笔记内容说明
  4. CentOS 7卸载mariadb安装mysql
  5. Python 循环语句(while, for)
  6. POI实现数据的导入
  7. ASPX一句话爆破工具
  8. Springboot- Caused by: org.hibernate.AnnotationException: No identifier specified for entity:
  9. Memcache面试题
  10. 配置web项目出的各种error (安装sql2008错误,网站连接数据库error错误等等)