HDU - 2051 Bitset 【进制转换】

Description

Give you a number on base ten,you should output it on base two.(0 < n < 1000)

Input

For each case there is a postive number n on base ten, end of file.

Output

For each case output a number on base two.

Sample Input

1
2
3

Sample Output

1
10
11
#include <cstdio>
#include <cstring>

using namespace std;

int a[10000];

int fun(int n) {
    int i = 0;
    while (n) {
        a[i] = n % 2;
        i++;
        n /= 2;
    }
    return i;
}

int main() {
    int n;

    while (scanf("%d", &n) != EOF) {
        if (0 == n)
            printf("0\n");
        else {
            int k = fun(n);
            for (int i = k-1; i >= 0; i--) {
                if (a[i] >= 10)
                    printf("%d", a[i]-10+'A');
                else
                    printf("%d", a[i]);
            }
            printf("\n");
        }
    }
    return 0;
}
发布了329 篇原创文章 · 获赞 342 · 访问量 17万+

猜你喜欢

转载自blog.csdn.net/Aibiabcheng/article/details/105336679