qfedu-basic-level/day7/d14.cpp

25 lines
385 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)
{
arr[len++] = n % 2 + 48; //// 将数字0或1转成 字符'0'或'1'; 48是'0'的ASCII码值
n /= 2;
}
while (len)
cout << arr[--len];
cout << endl;
return 0;
}