zoukankan      html  css  js  c++  java
  • 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);
    				break;
    			}
    		scanf("%d",&n);
    	}
    }</span>

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

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

  • 相关阅读:
    datagrid行拖拽(参考网上的相关资料)
    给定treeData,根据关键字进行过滤:显示父级元素;如果节点被选中,那它的子节点也全部被选中
    复选框
    UVA 10025 The ? 1 ? 2 ? ... ? n = k problem
    UVA10161 Ant on a Chessboard
    UVA 113 Power of Cryptography
    UVA 10785 The Mad Numerologist
    UVA 755 487-3279
    UVA10194 FootBall aka Soccer
    UVA 123 Searching Quickly 开始新的路程
  • 原文地址:https://www.cnblogs.com/Darknesses/p/12002585.html
Copyright © 2011-2022 走看看