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 ;
}

转载于:https://www.cnblogs.com/acsmile/archive/2011/05/04/2036561.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值