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. iOS 单例模式 学习 "52个方法 第6章 45条 使用 dispath_once 来执行只需运行一次的线程安全代码"

    百度定义:单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例. 维基百科:在软件工程中,单例是一种用于实现单例的数学概念,即将 ...

  2. JQuery 评分系统

              评分: ☆ ☆ ☆ ☆ ☆ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  3. Apache commons-io实现多文件读取和写入

    需求: "E:/data/"目录下有四个文件夹,如下: 每个文件夹下有几个.csv文件,如下: 将每个文件夹下的.csv文件合并成一个以该文件夹命名的.csv文件. 做法: 找到& ...

  4. Django 路由、模板和模型系统

    一.路由系统 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于这个URL调用这 ...

  5. 线性代数:A转置乘以A可逆

    如果A的列向量线性无关,则 T(A)*A得到一个可逆的方阵. 假设A是一个kxn的矩阵,那么T(A)*A是一个nxn的方阵:要证明这个方阵可逆,只要证明N(T(A)*A) = 零空间即可. 假设列向量 ...

  6. Java public class 与 class 区别

    在编写类的时候可以使用两种定义方式: public class 定义类 class 定义类 1.public class 定义类 如果一个类声明的时候使用了public class,则类名必须与文件名 ...

  7. 如何修改windows系统远程桌面默认端口

    此文档概述如何修改windows系统远程桌面的默认端口,众所周知windows系统默认的远程桌面端口是3389,这样对于开启远程桌面的计算机有一定的安全威胁,修改远程桌面的默认端口可以提高系统的安全性 ...

  8. HDU 2480 Steal the Treasure (并查集+贪心)

    题意:给你n个点,m条边,包括有向边与无向边,每条边都有一个权值.在每个点上都有一个人,他可以走与这个点直接相连的所有边中任意一条边一次,并且得到这个权值,就不能走了,注意这条路也只能被一个人走.问最 ...

  9. pf_ring DNA接收流程代码分析

    经过一个月的学习,对pf_ring DNA的内核部分有了一些认识,本文侧重pf_ring对ixgbe的改动分析. 先说一说接收流程吧,流程如下: 其中,硬中断处理函数是ixgbe_msix_clean ...

  10. review14

    日期的格式化 Formatter类的format方法: format(格式化模式, 日期列表) 按着“格式化模式”返回“日期列表”中所列各个日期中所含数据(年,月,日,小时等数据)的字符串表示.“格式 ...