pku 1220--NUMBER BASE CONVERSION(高精度进制转换)

    技术2022-05-19  23

    NUMBER BASE CONVERSIONTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 3000 Accepted: 1277DescriptionWrite a program to convert numbers in one base to numbers in a second base. There are 62 different digits: { 0-9,A-Z,a-z } HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when you get back to the original base, you should get the original number. InputThe first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines will have a (decimal) input base followed by a (decimal) output base followed by a number expressed in the input base. Both the input base and the output base will be in the range from 2 to 62. That is (in decimal) A = 10, B = 11, ..., Z = 35, a = 36, b = 37, ..., z = 61 (0-9 have their usual meanings). OutputThe output of the program should consist of three lines of output for each base conversion performed. The first line should be the input base in decimal followed by a space then the input number (as given expressed in the input base). The second output line should be the output base followed by a space then the input number (as expressed in the output base). The third output line is blank. Sample Input862 2 abcdefghiz10 16 123456789012345678901234567890123456789016 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD235 23 333YMHOUE8JPLT7OX6K9FYCQ8A23 49 946B9AA02MI37E3D3MMJ4G7BL2F0549 61 1VbDkSIMJL3JjRgAdlUfcaWj61 5 dl9MDSWqwHjDnToKcsWE1S5 10 42104444441001414401221302402201233340311104212022133030Sample Output62 abcdefghiz2 1101110000010001011111001001011001111100100110001101001000110 123456789012345678901234567890123456789016 3A0C92075C0DBF3B8ACBC5F96CE3F0AD216 3A0C92075C0DBF3B8ACBC5F96CE3F0AD235 333YMHOUE8JPLT7OX6K9FYCQ8A35 333YMHOUE8JPLT7OX6K9FYCQ8A23 946B9AA02MI37E3D3MMJ4G7BL2F0523 946B9AA02MI37E3D3MMJ4G7BL2F0549 1VbDkSIMJL3JjRgAdlUfcaWj49 1VbDkSIMJL3JjRgAdlUfcaWj61 dl9MDSWqwHjDnToKcsWE1S61 dl9MDSWqwHjDnToKcsWE1S5 421044444410014144012213024022012333403111042120221330305 4210444444100141440122130240220123334031110421202213303010 1234567890123456789012345678901234567890SourceGreater New York 2002

     

     

    #include <iostream> #include <stack> using namespace std; int Tmap[125]; //字符整形对照表 char Smap[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //整形字符对照表 char ch[1010]; char ans[1010]; int main() { int n,m,t,i; //inti() for(i='0';i<='9';i++) Tmap[i]=i-'0'; for(i='A';i<='Z';i++) Tmap[i]=i-55; for(i='a';i<='z';i++) Tmap[i]=i-61; scanf("%d",&t); while(t--) { scanf("%d%d%s",&n,&m,ch); printf("%d %s/n",n,ch); if(n==m) { printf("%d %s/n/n",n,ch); continue; } stack<char>q; int len=strlen(ch); int temp=0,d=0,s; int k=0; while(1) { s=Tmap[ch[k]]; for(i=k;i<len;i++) //高精度% { ch[i]=Smap[s/m]; d=s%m; if(i<len-1) s=Tmap[ch[i+1]]+d*n; } q.push(Smap[d]); //余数入栈 while(ch[k]=='0') //去掉多余的前置0 k++; if(k>=len) break; } printf("%d ",m); while(!q.empty()) { printf("%c",q.top()); q.pop(); } printf("/n/n"); } return 0; }

     

     


    最新回复(0)