题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/0337e32b1e5543a19fa380e36d9343d7
#include <iostream> #include "stack" #include "vector" using namespace std; string divide(string str) { string result; int remain = 0; for (int i = 0; i < str.size(); i++) { int temp = remain * 10 + str[i] - '0'; char tempChar='0'+temp/2; // str[i] = temp / 2 + '0'; remain =( str[i]-'0')% 2; result+=tempChar; } int pos = 0; while (str[pos] == '0') { pos++; } if(result=="0") return "";//如果只有一个零,调用substr后还是会返回零 else return result.substr(pos); } int main() { string a; while (cin >> a) { // 注意 while 处理多个 case // cout << a + b << endl; stack<int> bianryNums; vector<int> binary; while (a.size() > 0) { bianryNums.push((a[a.size() - 1] - '0') % 2); // binary.push_back((a[a.size() - 1] - '0') % 2); a = divide(a); } while(!bianryNums.empty()){ cout<<bianryNums.top(); bianryNums.pop(); } // for (int i = binary.size() - 1; i >= 0; i--) { // cout << binary[i]; // } cout << endl; } } // 64 位输出请用 printf("%lld")