现在的位置: 首页 > 综合 > 正文

poj 2453(二进制1的个数)

2014年02月20日 ⁄ 综合 ⁄ 共 372字 ⁄ 字号 评论关闭

题目链接: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;
}

 

抱歉!评论已关闭.