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
Source
【解析】#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char ss[110000];
int t[110000];
int ans[110000];
int main()
{
int T;scanf("%d",&T);
while(T--)
{
memset(t,0,sizeof(t));
memset(ans,0,sizeof(ans));
int n,m;
scanf("%d%d%s",&n,&m,ss+1);
int len=strlen(ss+1);
for(int i=len;i>=1;i--)
{
if(ss[i]>='A' && ss[i]<='Z') t[len-i+1]=ss[i]-'A'+10;//A=10;B=11…
if(ss[i]>='a' && ss[i]<='z') t[len-i+1]=ss[i]-'a'+36;//a=36;b=37…
if(ss[i]>='0' && ss[i]<='9') t[len-i+1]=ss[i]-'0';
}
int k=0;
while(len!=0)
{
for(int i=len;i>1;i--)
{
t[i-1]+=t[i]%m*n;
t[i]/=m;
}
ans[++k]=t[1]%m;
t[1]/=m;
while(len>0 && !t[len-1]) len--;
}
printf("%d %s\n",n,ss+1);
printf("%d ",m);
for(int i=k;i>=1;i--)
{
if(ans[i]>=36) printf("%c",ans[i]-36+'a');
if(ans[i]>=10 && ans[i]<=35) printf("%c",ans[i]-10+'A');
if(ans[i]<10) printf("%d",ans[i]);
}
printf("\n\n");
}
return 0;
}
/* 举个例子:50,要从十进制转换为二进制。
十位是5,个位是0,那么首先5/2商为2,余1
下一步就是1*10+0=10,然后个位就变成了10,然后10/2=5余0,0就是结果的个位,
然后下一步就是对25进行操作
2/2商1余0,那么十位就是1,个位就是0*10+5=5.
5/2商2余1,那么结果的十位就是1。
然后对12进行操作,十位是1,1/2商0余1,那么十位就为0了。
个位就是1*10+2=12.12/2商6余0,结果的百位就是0.
因为十位是0,所以只对个位进行操作了,6/2商3余0,千为就是0
3/2商1余1,万位为1.
1/2商0余1,十万位为1,所以50转换为二进制就是110010 */