天天看点

Bitset(HDU-2051)Problem DescriptionInputOutputSample InputSample OutputSource Program

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

Sample Output

1

10

11

题意:给出一个十进制数 n,将其转为二进制后输出

Source Program

#include<iostream>
using namespace std;
int main()
{
    int n;
    int a[100];
    int i,j;

    while(cin>>n)
    {
        i=0;//位数计数清零  
        while(n)//进制转换,每一位放在a[i]中
        {
            a[i]=n%2;
            n=n/2;
            i++;
        }
        for(j=i-1;j>=0;j--)//从低位取出,逆序输出
            cout<<a[j];
        cout<<endl;
    }
    return 0;
}
           
上一篇: hdu2051 bitset
下一篇: HDOJ 2051 Bitset

继续阅读