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 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.

 样例输入1
2
3
4
78
0
样例输出2
4
5
8
83
 题目大意: 一个2进制数,比如5------101 共有两个1,找一个比其大的,并且其二进制数也有一样多个1的最小数

 

这问题用枚举,就是一道Easy Problem,一直用函数枚举,很轻松~~~~~  代码如下:
<span style="font-size:12px;BACKGROUND-COLOR: #ffff99">#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
int find(int x)
{
	int a=0;
	while(x)
	{
		if(x%2)
			a++;
		x/=2;
	}
	return a;
}
int main()
{
	int n,a=0;
	scanf("%d",&n);
	while(n)
	{
		a=0;
		int x=n;
		while(x)
		{
			if(x%2)
				a++;
			x/=2;
		}
		for(n++;;n++)
			if(find(n)==a)
			{
				printf("%d\n",n);
				break;
			}
		scanf("%d",&n);
	}
}</span>

因为有多组数据,所以记得清零

可以,这很贪心,哈哈哈哈哈哈哈!

posted @ 2016-08-26 16:57  Izayoi_Doyo  阅读(151)  评论(0编辑  收藏  举报