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
思路:十进制转换为二进制,只要n/2不为零,就一直进行除二操作,然后倒着输出每一步的余数。
代码:
#include <iostream>
#include<string.h>
using namespace std;
int bn[16];
int main()
{
int n = 0;
while(cin >> n){
memset(bn,0,sizeof(bn));
int i = 0;
bn[i] = n % 2;
n = n/2;
while(n){
i++;
bn[i] = n%2;
n = n/2;
}
for(int j = i;j >= 0;j --){
cout << bn[j];
}
cout << endl;
}
return 0;
}