题目链接:http://poj.org/problem?id=2453
题目意思:就是先把n翻成二进制数,然后统计其中1的个数,求出(枚举)与他一样的1的个数,且比他大的最小的数
代码:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
int count(int x) {
int ret = 0;
while (x > 0) {
if (x % 2 == 1)
ret++;
x /= 2;
}
return ret;
}
int main() {
int n;
while (scanf("%d", &n) != EOF && n != 0) {
int cnt = count(n);
while (n++)
if (cnt == count(n)) {
printf("%d\n", n);
break;
}
}
return 0;
}