#include <iostream> #include <string> #include <stack> using namespace std; string Divide(string s, int x) { int remainder = 0; for (int i = 0; i < s.size(); i++) { int cur = remainder * 10 + s[i] - '0'; s[i] = cur / x + '0'; remainder = cur % x; } int pos = 0; while (s[pos] == '0') { pos++; } return s.substr(pos); } string convertFunc1(string s, int x) { stack<int> sck; while (s.size() != 0) { int bit = (s[s.size() - 1] - '0') % 2; sck.push(bit); s = Divide(s, x); } string res; while (!sck.empty()) { res.push_back(sck.top() + '0'); sck.pop(); } return res; } string Multiple(string str, int x) { //字符串乘法 int carry = 0; //进位 for (int i = str.size() - 1; i >= 0; i--) { int cur = x * (str[i] - '0') + carry; str[i] = cur % 10 + '0'; carry = cur / 10; } if (carry != 0) { str = "1" + str; } return str; } string Add(string str, int x) { //字符串加法 int carry = x; for (int i = str.size() - 1; i >= 0; i--) { int cur = (str[i] - '0') + carry; str[i] = cur % 10 + '0'; carry = cur / 10; } if (carry != 0) { str = "1" + str; } return str; } string convertFunc2(string s, int x) { string ans = "0"; for (int i = s.size() - 1; i >= 0; i--) { //从最低二进制位开始 ans = Multiple(ans, x); ans = Add(ans, s[i] - '0'); } return ans; } int main() { string s; while (cin >> s) { cout << convertFunc2(convertFunc1(s, 2), 2); } return 0; }
不太懂,留着学习