1223:An Easy Problem

【题目描述】

给定一个正整数N,求最小的、比N大的正整数M,使得M与N的二进制表示中有相同数目的1。

举个例子,假如给定的N为78,其二进制表示为1001110,包含4个1,那么最小的比N大的并且二进制表示中只包含4个1的数是83,其二进制是1010011,因此83就是答案。

【输入】

输入若干行,每行一个数n(1≤n≤1000000),输入"0"结束。

【输出】

输出若干行对应的值。

【输入样例】

1
2
3
4
78
0

【输出样例】

2
4
5
8
83
// Created on 2020/2/11

/*#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <climits>*/
#include <bits/stdc++.h>

using namespace std;

const int idata=10000+5;
int n,t;
int step[idata];
int flag;
bool judge;

inline int solve(int a)
{
    int cnt=0;
    while(a)
    {
        if(a & 1==1)
            cnt++;
        a>>=1;
    }
    return cnt;
}

int main()
{
    int temp;
    while(cin>>n&&n)
    {
        temp=solve(n);
        t=n+1;
        while(temp!=solve(t))
            t++;
        cout<<t<<endl;
    }
    return 0;
}
发布了177 篇原创文章 · 获赞 8 · 访问量 6335

猜你喜欢

转载自blog.csdn.net/C_Dreamy/article/details/104285451