POJ 1220 大数任意进制转换

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. 

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
 
题意就是说将M进制的大数转换成N进制。高精度不熟悉,参考洞庭散人的一篇文章浅谈大数的进制转换
感谢散人。
AC代码:
#include"stdio.h"
#include
"string.h"
char str[1000];
int start[1000],ans[1000],res[10000];//被除数 商 余数
int base1,base2;
void change()
{
int i,len=strlen(str);
start[
0]=len;
for(i=1;i<=len;i++)
{
if(str[i-1]>='0'&&str[i-1]<='9')
start[i]
=str[i-1]-'0';
else if(str[i-1]>='a'&&str[i-1]<='z')
start[i]
=str[i-1]-'a'+36;
else
start[i]
=str[i-1]-'A'+10;
}
}
void solve()
{
memset(res,
0,sizeof(res));
int y,i,j;
//先余低位,后余高位
while(start[0]>=1)//知道被除数等于0
{
y
=0;i=1;
ans[
0]=start[0];
while(i<=start[0])
{
y
=y*base1+start[i];
ans[i
++]=y/base2;
y
%=base2;
}
res[
++res[0]]=y;
i
=1;
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()
{
printf(
"%d %s\n%d ",base1,str,base2);
int i;
for(i=res[0];i>=1;i--)
{
if(res[i]>=0&&res[i]<=9)
printf(
"%d",res[i]);
else if(res[i]>=10&&res[i]<=35)
printf(
"%c",'A'+res[i]-10);
else printf("%c",'a'+res[i]-36);
}
printf(
"\n\n");
}
int main()
{
int n;
//freopen("a.in","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&n);
while(n--)
{
scanf(
"%d%d%s",&base1,&base2,str);
change();
solve();
output();
}
return 0;
}

posted @ 2011-05-04 15:31  Ac_smile  阅读(586)  评论(0编辑  收藏  举报