NUMBER BASE CONVERSION
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4268 | Accepted: 1919 |
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
分析:字符串处理,本题是一个不断求余的问题,仔细想一下可知,此大数最后一位即自己余所求进制,举例,121十进制转换为二进制,121%2=1,1即为最后一位,60%2=0,0即为倒数第二位,模拟此过程。每次求的余数d就将其push进stack中,最后输出即可。
新的问题又出来了,本题数据不可能直接拿来求余,解决这个问题就要用到数论的简单知识了,((a*n)+k)%n=(a+k%n)%n;
不知道此公式的同学找数字演算下,其实仔细推下也可以推出来,((3*97)+15)%97 = 3+15%97(这里为了容易看出规律,省略了点东西)。
接下来,n进制数可表示为n*(第一位)+(剩余数位),再结合上面公式,再加一点耐心,就可以做出代码了。
给出AC代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rev(i,a,b) for(int i=(a);i>=(b);i--)
#define clr(a,x) memset(a,x,sizeof a)
const int maxn=1005;
const int maxm=4005;
const int mod=1e9 +7;
char dit[maxn],r[maxn];
void chang(char *ss,int n,char *r,int m)
{
char s[maxn];memcpy(s,ss,sizeof s);
map<char,int>mp;
char smp[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for(int i='0';i<='9';i++)
mp[i]=i-'0';
for(int i='A';i<='Z';i++)
mp[i]=i-'A'+10;
for(int i='a';i<='z';i++)
mp[i]=i-'a'+36;
int len=strlen(s),cnt=0;
for(int k=0;k!=len;)
{
int d=0;
for(int i=k;i<len;i++)
{
int cur=(mp[s[i]]+d*n);
d=cur%m;
s[i]=smp[cur/m];
}
r[cnt++]=smp[d];
while(s[k]=='0')
k++;
}
r[cnt]='\0';
//strrev(r);
for(int i=0,j=cnt-1;i<j;i++,j--)
{
char c=r[i];
r[i]=r[j];
r[j]=c;
}
}
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%s",&n,&m,dit);
chang(dit,n,r,m);
printf("%d %s\n%d %s\n\n",n,dit,m,r);
}
return 0;
}