HDOJ2051:Bitset

Problem 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

一道看起来高端的水题。AC代码:

#include
#include
int main()
{
    int two[1000], cache, len, i;
    while (~scanf("%d", &cache))
    {
        memset(two, 0, sizeof(two));
        i = 0;
        while (cache !=0)
        {
            two[i] = cache % 2;
            cache = cache / 2;
            i++;
        }
        len = i;
        for (i = (len-1); i >= 0; i--)
        {
            printf("%d", two[i]);
        }
        printf("n");
    }
    return 0;
}

一开始是直接用的strlen()函数,一直报错,郁闷……犯这种低级错误。( ̄﹏ ̄;)

最后也算欢乐的一次过了吧。(-。-;)

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注