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 ;
}

an easy problem(贪心)的更多相关文章

  1. [openjudge] 1455:An Easy Problem 贪心

    描述As we known, data stored in the computers is in binary form. The problem we discuss now is about t ...

  2. 一本通 1223:An Easy Problem

    \[传送门qwq\] [题目描述] 给定一个正整数N,求最小的.比N大的正整数M,使得M与N的二进制表示中有相同数目的1. 举个例子,假如给定的N为78,其二进制表示为1001110,包含4个1,那么 ...

  3. NOI4.6 1455:An Easy Problem

    描述 As we known, data stored in the computers is in binary form. The problem we discuss now is about ...

  4. UVA-11991 Easy Problem from Rujia Liu?

    Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...

  5. An easy problem

    An easy problem Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  6. UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)

    Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...

  7. POJ 2826 An Easy Problem?!

    An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7837   Accepted: 1145 ...

  8. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

  9. 【暑假】[实用数据结构]UVa11991 Easy Problem from Rujia Liu?

    UVa11991 Easy Problem from Rujia Liu?  思路:  构造数组data,使满足data[v][k]为第k个v的下标.因为不是每一个整数都会出现因此用到map,又因为每 ...

随机推荐

  1. str字符串、bool类型常用方法总结

    字符串拼接 必须是字符串与字符串拼接 print('马化腾'+'马云') print('马化腾' * 10) 将打印10个马化腾 字符串翻转 [ : :-1] 字符串可以加和乘,不能减和乘 input ...

  2. iOS 9 的新功能 universal links

    什么是 universal links: (通用链接) 一种能够方便的通过传统 HTTP 链接来启动 APP, 使用相同的网址打开web page和 APP的方式. 第一点,链接打开网址 顾名思义 第 ...

  3. 015_[小插曲]看黄老师《炼数成金Hadoop应用开发实战案例》笔记

    1.大数据金字塔结构 Data Source-->Data Warehouses/Data Marts-->data exploration-->Data Mining-->D ...

  4. java 图片Base64字符串转图片二进制数组

    public static byte[] base64ToImgByteArray(String base64) throws IOException{ sun.misc.BASE64Decoder ...

  5. Linux Shell基础 Shell的输入重定向和输出重定向

    概述 在 Linux 中输入设备指的是键盘,输出设备指的是显示器.在 Linux 中,所有的内容都是文件,计算机硬件也是文件,标准输入设备(键盘)和标准输出设备(显示器)也是文件.这些设备的设备文件名 ...

  6. Js 类型方面的神坑

    你有没有遇见过本来好好的一个数组结果 typeof 出来是个 object 的情况,你有没有遇到过非要写个 typeof x === "undefined" 判断未赋值的情况... ...

  7. bootstrap 模态框中弹出层 input不能获得焦点且不可编辑

    bootstrap 模态框中弹出层 input不能获得焦点且不可编辑 问题描述:bs框架支持一层model层的情况下,在模态框中弹出了自定义的弹出层.发现自定义弹出层的输入框不能获得焦点且不可编辑. ...

  8. php数组函数-array_merge()

    array_merge()函数把两个或多个数组合并为一个数组. 如果键名有重复,该键的键值为最后一个键名对应的值.如果数组是数字 索引,则键名会以连续方式重新索引. 注:如果仅仅向array_merg ...

  9. 九、搭建备份服务器 使用rsync服务

    简介 Rsync是开源快速.多功能,可以实现全量和增量的本地或者远程数据同步备份的优秀工具.增量备份效率更高,可以同步内容也可以同步属性 [root@backup-41 ~]# rpm -qa rsy ...

  10. 服务器windows 2003 安装SQL 2000+SP4

    (在windows 2003安装SQL 2000,对于03系统是没有32位和64位之分的) (32位系统需要重启机器,64不需要) 在站点下载数据库 解压后有两个文件夹 首先安装SQL2000 第1步 ...