题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/0337e32b1e5543a19fa380e36d9343d7
#include <iostream> #include<vector> //参考答案解法,磕磕绊绊,还不熟练 using namespace std; const int MaxNum = 30; string Divide(string str, int x) { int remainder = 0; for (int i = 0; i < str.size(); i++) { int current = remainder * 10 + str[i] - '0'; str[i] = current / x + '0'; //4 2 1 remainder = current % x; } int pos = 0; while (str[pos] == '0') { pos++; } return str.substr(pos); } int main() { string str; while (getline(cin, str)) { //8 vector<int> binary; while (str.size() != 0) { int last = str[str.size() - 1] - '0'; binary.push_back(last % 2); str = Divide(str, 2); } for (int i = binary.size() - 1; i >= 0; i--) { printf("%d", binary[i]); } printf("\n"); } }