31 lines
469 B
C++
31 lines
469 B
C++
|
// 输入一个正整数,输出它的二进制表示
|
||
|
#include <iostream>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
char arr[32] = "";
|
||
|
int len = 0;
|
||
|
int n;
|
||
|
cout << "请输入一个数:";
|
||
|
cin >> n;
|
||
|
|
||
|
// 逐位取出
|
||
|
while (n)
|
||
|
{
|
||
|
if (n & 1)
|
||
|
arr[len++] = '1';
|
||
|
else
|
||
|
arr[len++] = '0';
|
||
|
n >>= 1;
|
||
|
}
|
||
|
|
||
|
// 逆序输出
|
||
|
while (len)
|
||
|
cout << arr[--len];
|
||
|
|
||
|
cout << endl;
|
||
|
|
||
|
return 0;
|
||
|
}
|