NUMBER BASE CONVERSION
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3231 Accepted: 1394

Description

Write 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.

Input

The 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).

Output

The 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 Input

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

Sample Output

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

Source

 
/*
高精度进制转换
把oldBase 进制的数转化为newBase 进制的数输出。
调用方法,输入str, oldBase newBase.
change();
solve();
output();
也可以修改output(),使符合要求,或者存入另外一个字符数组,备用
*/
#include
<stdio.h>
#include
<string.h>
#define MAXSIZE 1000
char str[MAXSIZE];//输入字符串
int start[MAXSIZE],ans[MAXSIZE],res[MAXSIZE];//被除数,商,余数
int oldBase,newBase;//转换前后的进制

//单个字符得到数字
int getNum(char c)//这里进制字符是先数字,后大写字母,后小写字母的
{
if(c>='0'&&c<='9') return c-'0';//数字
if(c>='A'&&c<='Z') return c-'A'+10;//大写字母
return c-'a'+36;//小写字母
}
//数字得到字符
char getChar(int i)
{
if(i>=0&&i<=9)return i+'0';
if(i>=10&&i<=35)return i-10+'A';
return i-36+'a';
}
void change()//把输入的字符串的各个数位还原为数字形式
{
int i;
start[
0]=strlen(str);//数组的0位存的是数组长度
for(i=1;i<=start[0];i++)
start[i]
=getNum(str[i-1]);
}
void solve()
{
memset(res,
0,sizeof(res));//余数位初始化为空
int y,i,j;
while(start[0]>=1)
{
y
=0;i=1;
ans[
0]=start[0];
while(i<=start[0])
{
y
=y*oldBase+start[i];
ans[i
++]=y/newBase;
y
%=newBase;
}
res[
++res[0]]=y;//这一轮得到的余数
i=1;//找下一轮商的起始处,去掉前面的0
while(i<=ans[0]&&ans[i]==0) i++;
memset(start,
0,sizeof(start));
for(j=i;j<=ans[0];j++)
start[
++start[0]]=ans[j];
memset(ans,
0,sizeof(ans));
}
}
void output()//从高位到低位逆序输出
{
int i;
printf(
"%d %s\n",oldBase,str);
printf(
"%d ",newBase);
for(i=res[0];i>=1;i--)
printf(
"%c",getChar(res[i]));
printf(
"\n\n");
}
int main()
{
//freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
int T;
scanf(
"%d",&T);
while(T--)
{
scanf(
"%d %d %s",&oldBase,&newBase,str);
change();
solve();
output();
}
return 0;
}