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. Loadrunder之脚本篇——参数化同行取值

    select next row 记录选择方式 Same line as,这个选项只有当参数多余一个时才会出现,其作用是根据某一个参数的行号取同一行. 例中的做法如下: 将多个参数存放在一个参数文件中: ...

  2. js对象属性方法大总结(收集)

    数组(Array):系列元素的有序集合: 详细演示请看:[js入门系列演示·数组 ] http://www.cnblogs.com/thcjp/archive/2006/08/04/467761.ht ...

  3. windows下载Mysql-python

    Mysql-python第三方模块官方不支持windows系统,而国外大学提供了非官方 的支持windows系统的模块,可前往 https://www.lfd.uci.edu/~gohlke/pyth ...

  4. TIJ读书笔记03-初始化和构造器

      TIJ读书笔记03-初始化和构造器 初始化和清理是涉及安全的两个问题,如果对象不能正确的初始化会引起很多错误,比如空指针异常等,如果不能恰当及时的清理,会占用过多资源. 构造器在创建一个类的实例的 ...

  5. OpenCV图片拼接的两种方法

    https://my.oschina.net/xiaot99/blog/226589 一.原图 1.jpg                                        2.jpg   ...

  6. Sublime Text3 打开文档乱码

    一.安装包管理器使用Ctrl+~快捷键或者通过View->Show Console菜单打开命令行,粘贴如下代码 import urllib.request,os; pf = 'Package C ...

  7. 主攻ASP.NET MVC4.0之重生:CheckBoxListHelper和RadioBoxListHelper的使用

    在项目中新建Helpers文件夹,创建CheckBoxListHelper和RadioBoxListHelper类. CheckBoxListHelper代码 using System; using ...

  8. 主攻ASP.NET.4.5 MVC4.0之重生:图书推荐

    前段时间看完ASP.Net4.0 框架揭秘 ,目前现在此书在家睡大觉,看得云里雾里,实战有些东西用不上,感觉好可惜. 大概看了一下这本书,这本书很多功能,可以在实际项目中能用上的方法和技巧.小小推荐, ...

  9. console.log()方法中%s的作用

    一.console.log("log信息"); 二.console.log("%s","first","second") ...

  10. 20165101刘天野 2017-2018-2 《Java程序设计》第6周学习总结

    #20165101刘天野 2017-2018-2 <Java程序设计>第6周学习总结 教材学习内容总结 第八章:常用实用类 String类:不可变类,一些看起来能够改变String的方法其 ...