题解 | #进制转换#
进制转换
https://www.nowcoder.com/practice/0337e32b1e5543a19fa380e36d9343d7
#include <iostream> #include <string> using namespace std; string bigIntMod(string num,int h){ int x; int n = num.size(); if (n>1){ x = (num[n-2]-'0')*10 + (num[n-1]-'0'); } else{ x = stoi(num); } return to_string(x%h); } string bigIntDiv(string num,int h){ string res,ans; int x=0,y=0; for (int i = 0; i < num.size(); ++i) { x = y*10 + (num[i] - '0'); if (x>=h){ res += to_string(x/h); } else{ res += "0"; } y = x % h; } int i = 0; while (res[i]=='0'){ i++; } int l = res.size()-i; for (int j = 0; j < l; ++j) { ans += res[i++]; } return ans; } string trans(string num,int h){ string res,w; while (num.size()){ w = bigIntMod(num,h); num = bigIntDiv(num,h); res = w + res; if (num.size()==1 && num[0]=='0') break; } return res; } int main(){ string str; while (cin>>str){ cout<<trans(str,2)<<endl; } return 0; }