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.
{ 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
输入:
第一行一个整数t,表示有多少测试数据,接下来每一行三个数,第一个是整数n即原进制基数,第二个是整数m即要转换成的进制基数,第三个数(数字与字母混合成的)是要转换的数。进制基数在2到62之间,其中0=0,1=1=2,……,8=8,9=9,A=10,B=11,……,Y=34,Z=35,a=36,b=37,……,y=60,z=61。
输出:
输出有三行,第一行是原进制的基数和原数,两者之间有一个空格;第二行是转换后的进制基数和对应数,两者之间也有一个空格;第三行是空行。
解题思路:这是一个关于大数之间的进制转换,即高精度的进制转换,有一定的规律,请详看http://blog.youkuaiyun.com/yanghuaqings/article/details/38366731
这里我仅贴上相关的代码
代码如下:
代码一:C/C++代码
#include <stdio.h>
#include <string.h>
const int maxn = 1000;
int t[maxn], A[maxn];
char str1[maxn], str2[maxn];
int n, m;
void solve()
{
int i, len, k;
len = strlen(str1);
for(i=len; i>=0; --i)
t[len-1-i] = str1[i] -(str1[i]<58 ? 48: str1[i]<97 ? 55: 61);
for(k=0; len;)
{
for(i=len; i>=1; --i)
{
t[i-1] +=t[i]%m*n;
t[i] /= m;
}
A[k++] = t[0] % m;
t[0] /=m;
while(len>0&&!t[len-1])
len--;
}
str2[k] =NULL;
for(i=0; i<k; i++)
str2[k-1-i] = A[i]+(A[i]<10 ? 48: A[i]<36 ? 55:61);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%s",&n, &m, str1);//将n进制的数str转换为m进制
solve();
printf("%d %s\n%d %s\n\n", n, str1, m, str2);
}
return 0;
}
代码二:C/C++代码
#include <iostream>
#include <stack>
#include<stdio.h>
#include<string.h>
using namespace std;
int Tmap[125]; //字符整形对照表
char Smap[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //整形字符对照表
char ch[1010];
char ans[1010];
int main()
{
int n,m,t,i;
for(i=0;i<62;i++)
{
if(i<10)
Tmap[Smap[i]]=Smap[i]-'0';
else if(i<36)
Tmap[Smap[i]]=Smap[i]-'A'+10;
else
Tmap[Smap[i]]=Smap[i]-'a'+36;
}
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]];//第k个字符在n进制下对应的整数值
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;
}
代码二中有一部分代码可互换:
for(i=0;i<62;i++)
{
if(i<10)
Tmap[Smap[i]]=Smap[i]-'0';
else if(i<36)
Tmap[Smap[i]]=Smap[i]-'A'+10;
else
Tmap[Smap[i]]=Smap[i]-'a'+36;
}
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;
代码三:C/C++代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 600//大数的最大位数
char str[MAXSIZE];//储存需要转换的大数,以字符形式储存
//oldbase:原基数;newbase:新基数;len:大数的长度
int oldbase,newbase,len,k;
//a[MAXSIZE];储存大数的最小整型,注意a[0]没有使用;r[MAXSIZE]:储存余数,即所求数的每一位
int a[MAXSIZE],r[MAXSIZE];
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 ChToNum()//调用函数将字符串转化成对应进制的整数,按位转化
{
len=strlen(str);//测量字符串的长度
//遍历每一个字符,一一对应转化,注意没有使用a[0]
for(int i=1;i<=len;i++)
{
a[i]= GetNum(str[i-1]);
}
}
void alter()//转化函数,核心代码
{
int m=1;
k=0;
while(m<=len)
{
int i,t = 0;
//对数的每一位除新基数求商求余
for(i=m;i<=len;i++)
{
t=t*oldbase+a[i];//计算除数
a[i]=t/newbase;//求商
t%=newbase;//求余
}
r[k++]=t;//记录最后一个余数
for(i=1;i<=len&&!a[i];i++);//去除前导0
m=i;
}
}
void print()//输出函数
{
printf("%d %s\n%d ",oldbase,str,newbase);
while(k--)//逆序输出余数,即是结果
{
printf("%c",GetChar(r[k]));
}
printf("\n\n");
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
scanf("%d %d %s",&oldbase,&newbase,str);
ChToNum();//字符转换为整型
alter();//转化
print();//函数调用输出结果
}
}
return 0;
}