10进制转换为N进制

进制转换

给定一个十进制整数N,求其对应2进制数中1的个数
Input
第一个整数表示有N组测试数据,其后N行是对应的测试数据,每行为一个整数。
Output
N行,每行输出对应一个输入。
Sample Input
4
2
100
1000
66
Sample Output
1
3
6
2


#include <stdio.h>
 
#define BASE 2
#define ONE 1
 
int count_digit(int n, int digit)
{
    int count;
 
    count = 0;
    while(n) {
        if(n % BASE == digit)
            count++;
        n /= BASE;
    }
 
    return count;
}
 
int main()
{
    int n, a;
 
    scanf("%d", &n);
    while(n--) {
        scanf("%d", &a);
 
        printf("%d\n", count_digit(a, ONE));
    }
 
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_19656301/article/details/82750772
今日推荐