题解 | #进制转换2#
进制转换2
https://www.nowcoder.com/practice/ae4b3c4a968745618d65b866002bbd32
#include <iostream> #include<algorithm> using namespace std; int chartoint(char c){ if('0'<=c&&c<='9') return c-'0'; if('A'<=c&&c<='Z') return c-'A'+10; if('a'<=c&&c<='z') return c-'a'+10; return 0; } char inttochar(int n){ if(0<=n&&n<=9) return n+'0'; else return n-10+'A'; } string change(string number,int presbase,int posbase){ string res=""; while(!number.empty()){ int remainder=0; for(int i=0;i<number.size();i++){ int cur = remainder*presbase + chartoint(number[i]); number[i] = inttochar(cur/posbase); remainder = cur%posbase; } res+=inttochar(remainder); while(number[0]=='0') number.erase(0,1); } reverse(res.begin(),res.end()); return res; } int main() { int m,n; string number; cin>>m>>n>>number; cout<<change(number,m,n); return 0; } // 64 位输出请用 printf("%lld")