题解 | 进制转换
#include<iostream>
#include<cmath>
using namespace std;
string chu(string n) { //实现n/2
int y, z, c = 0;
for (int i = 0; i < n.size(); i++) { //实现n/2
y = n[i] - '0' + c * 10;
z = y % 2;
y = y / 2;
if (z != 0) {
c = 1;
} else c = 0;
n[i] = y + '0';
}
if (n[0] == '0')n.erase(0, 1);
return n;
}
int main() {
string n;
string t;
cin >> n;
while (!n.empty()) {
int x = n[n.size() - 1] - '0';
if (x % 2 == 0) t = "0" + t;
else t = "1" + t;
int y, z, c = 0;
n = chu(n);
}
cout << t << endl;
t.clear();
}
查看2道真题和解析