qfedu-basic-level/day7/d13.cpp

31 lines
469 B
C++
Raw Normal View History

2023-06-21 14:30:56 +08:00
// 输入一个正整数,输出它的二进制表示
#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;
}