Write a program, can realize a number from one base to another base.
There are 62 different digits {0-9,A-Z,a-z}.
Input format
Enter an integer for the first row to represent the number of rows to follow.
Next, each line contains three numbers, first input base (decimal), then output base (decimal), and finally input number represented by input base. The numbers are separated by spaces.
Both input and output are in the range of 2 to 62.
(in decimal) A = 10, B = 11 ,Z = 35,a = 36,b = 37,… , z = 61 (0-9 still means 0-9).
Output format
For each set of base conversion, the output of the program consists of three lines.
The first line contains two numbers, first the input base (decimal) and then the input number in the input base.
The second line contains two numbers, first the output base (decimal) and then the input number in the output base.
The third line is blank.
The numbers in the same line are separated by spaces.
Input example:
8 62 2 abcdefghiz 10 16 1234567890123456789012345678901234567890 16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 35 23 333YMHOUE8JPLT7OX6K9FYCQ8A 23 49 946B9AA02MI37E3D3MMJ4G7BL2F05 49 61 1VbDkSIMJL3JjRgAdlUfcaWj 61 5 dl9MDSWqwHjDnToKcsWE1S 5 10 42104444441001414401221302402201233340311104212022133030
Output example:
62 abcdefghiz 2 11011100000100010111110010010110011111001001100011010010001 10 1234567890123456789012345678901234567890 16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 35 333YMHOUE8JPLT7OX6K9FYCQ8A 35 333YMHOUE8JPLT7OX6K9FYCQ8A 23 946B9AA02MI37E3D3MMJ4G7BL2F05 23 946B9AA02MI37E3D3MMJ4G7BL2F05 49 1VbDkSIMJL3JjRgAdlUfcaWj 49 1VbDkSIMJL3JjRgAdlUfcaWj 61 dl9MDSWqwHjDnToKcsWE1S 61 dl9MDSWqwHjDnToKcsWE1S 5 42104444441001414401221302402201233340311104212022133030 5 42104444441001414401221302402201233340311104212022133030 10 1234567890123456789012345678901234567890
Base conversion function:
void change(int a,string s1,int b,string &s2)//Convert s1 of a-Base to s2 of b-Base { vector<int>num; for(int i=0;i<s1.size();i++){ if(s1[i]<='9') num.push_back(s1[i]-'0'); else if(s1[i]<='Z') num.push_back(s1[i]-'A'+10); else num.push_back(s1[i]-'a'+36); } reverse(num.begin(),num.end());//From low to high vector<int>ans; while(num.size()){//Short division until quotient 0, i.e. num.empty int r=0;//r is the remainder of each short division operation for(int i=num.size()-1;i>=0;i--){//The whole cycle is a short division operation. The numbers in num are stored from the low to the high, and vice versa num[i]+=r*a;//Current divisor is equal to ((last remainder * a) + (number of current bits)) < = = > (decimal conversion)) r=num[i]%b;//Take this remainder as the base of the next dividend num[i]/=b;//Count and calculate the value of num after this operation } ans.push_back(r);//Take the last remainder of this operation as the value of this bit of the answer (here it is stored from low to high, because the short division is in reverse order) while(num.size()&&num.back()==0) num.pop_back();//Remove leading zeros (just because vector has only pop back and no pop front, it stores from low to high) } reverse(ans.begin(),ans.end());//Because ans is stored from low to high, so in the end, reverse for(int i=0;i<ans.size();i++){ if(ans[i]<=9) s2+=char(ans[i]+'0'); else if(ans[i]<=35) s2+=char(ans[i]-10+'A'); else s2+=char(ans[i]-36+'a'); } }
Full code:
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; void change(int a,string s1,int b,string &s2)//Convert s1 of a-Base to s2 of b-Base { vector<int>num; for(int i=0;i<s1.size();i++){ if(s1[i]<='9') num.push_back(s1[i]-'0'); else if(s1[i]<='Z') num.push_back(s1[i]-'A'+10); else num.push_back(s1[i]-'a'+36); } reverse(num.begin(),num.end());//From low to high vector<int>ans; while(num.size()){//Short division until quotient 0, i.e. num.empty int r=0;//r is the remainder of each short division operation for(int i=num.size()-1;i>=0;i--){//The whole cycle is a short division operation. The numbers in num are stored from the low to the high, and vice versa num[i]+=r*a;//Current divisor is equal to ((last remainder * a) + (number of current bits)) < = = > (decimal conversion)) r=num[i]%b;//Take this remainder as the base of the next dividend num[i]/=b;//Count and calculate the value of num after this operation } ans.push_back(r);//Take the last remainder of this operation as the value of this bit of the answer (here it is stored from low to high, because the short division is in reverse order) while(num.size()&&num.back()==0) num.pop_back();//Remove leading zeros (just because vector has only pop back and no pop front, it stores from low to high) } reverse(ans.begin(),ans.end());//Because ans is stored from low to high, so in the end, reverse for(int i=0;i<ans.size();i++){ if(ans[i]<=9) s2+=char(ans[i]+'0'); else if(ans[i]<=35) s2+=char(ans[i]-10+'A'); else s2+=char(ans[i]-36+'a'); } } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin>>t; while(t--){ int a,b; string s1,s2; cin>>a>>b>>s1; change(a,s1,b,s2); cout<<a<<" "<<s1<<endl; cout<<b<<" "<<s2<<endl; cout<<endl; } return 0; }