An Easy Problem --- 贪心
来源:互联网 发布:淘宝3c证书编号是什么 编辑:程序博客网 时间:2024/05/17 05:05
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
- 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". - 输入
- 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. - 输出
- One integer per line, which is J.
- 样例输入
1234780
- 样例输出
2458
83
题意:
随意输入一个字,比如78他的二进制是:1001110,要求输出一个数比这个78大的并且二进制数中1的个数是相同的最小的一个。
解题分析:
这用的是贪心法,每次求一的个数,然后每次在原来的数上加一,看看哪个数复合。
代码:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int p, x, num1, num2;
while(1)
{
num1 = num2 = 0;
scanf("%d", &x);
if(!x)
break;
p = x;
while(p > 0)
{
if(p%2 != 0)
num1++;
p >>= 1;
}
while(1)
{
x++;
p = x;
num2 = 0;
while(p > 0)
{
if(p % 2 != 0)
num2++;
p >>= 1;
}
if(num2 == num1)
{
printf("%d\n", x);
break;
}
}
}
return 0;
}
0 0
- An Easy Problem --- 贪心
- Openjudge 4.6之贪心 1455:An Easy Problem
- 1455:An Easy Problem(4.6算法之贪心)
- An Easy Problem
- 2055 An easy problem
- An Easy Problem!
- An Easy Problem!
- poj2826 An Easy Problem?!
- HDOJ2055 An easy problem
- An Easy Problem
- hdu-An Easy Problem
- hdu2601 An easy problem
- An Easy Problem
- hdu-An Easy Problem
- HDU_2055 An easy problem
- HDU_2123 An easy problem
- B - An easy problem
- B - An easy problem
- Delphi XE7的蓝牙 Bluetooth
- 适合于图像处理方向的SCI期刊杂志列表【转】
- jQuery常用插件
- Hibernate hql
- Android webview使用详解
- An Easy Problem --- 贪心
- HDU5105 - Math Problem(一元三次方程求最大值(直接暴力))
- 内核进程的堆栈
- Cygwin 初步配置和使用
- nginx扩展功能
- 如何编写一个shell脚本
- Android雷达扫描
- SQL SERVER 2008 R2的累计补丁下载地址
- 一句话计算复选框选中个数