题解 | #10进制VS2进制# 10进制以内转换模板
10进制 VS 2进制
https://www.nowcoder.com/practice/fd972d5d5cf04dd4bb4e5f027d4fc11e
#include <algorithm> #include <iostream> #include <iterator> using namespace std; string s; string transform(int m, int n, string num) { int remain; string res = ""; for(int i = 0; i < num.size(); ) { remain = 0; for(int j = i; j < num.size(); j++) { int k = (remain * m + num[j] - '0') % n; num[j] = (remain * m + num[j] - '0') / n + '0'; remain = k; } res += char(remain + '0'); while(num[i] == '0') i++; } reverse(res.begin(), res.end()); return res; } int main() { while(cin >> s) { string s1 = transform(10, 2, s); reverse(s1.begin(), s1.end()); string res = transform(2, 10, s1); cout << res << endl; } }