大数数制转换
#include<iostream> #include<string> using namespace std; int a[100]; string Convert(string s,int m,int n){ for (int i=0;i<s.length();i++){ if (s[i]>='0' && s[i]<='9'){ a[i] = s[i] - '0'; }else{ a[i] = s[i] - 'a' + 10; } //sum = sum*m + a[i]; //数太大会爆掉,要留到下个循环,边累加边取余 } string ans; while (true){ for(int i=0;i<s.length();i++){ cout<<a[i]<<" "; } cout<<endl; int flag = 0; for(int i=0;i<s.length();i++){ if(a[i]!=0){ flag = 1; break; } } if(flag == 0){ break; } long long sum = 0; for(int i=0;i<s.length();i++){ //每过一次while循环 sum = sum*m+a[i]; sum = sum%n; } if(sum>=0 && sum<=9){ char ch = sum + '0'; ans += ch; }else{ char ch = sum-10+'a'; ans += ch; } int point=0; for(int i=0;i<s.length();i++) //作用:将数除以n; 比如123456789 -> 003429355 { a[i] += point*m; point = a[i]%n; a[i] /= n; } } for(int i=0,j=ans.length()-1;i<j;i++,j--){ char ch = ans[i]; ans[i] = ans[j]; ans[j] = ch; } return ans; } int main() { int m,n; string s; while(cin>>m){ cin>>n; cin>>s; for(int i=0;i<s.length();i++){ if(s[i]>='A' && s[i]<='Z'){ s[i] = s[i]+32; } } cout<<Convert(s,m,n)<<endl; } return 0; }