NUMBER BASE CONVERSION (进制转化) (模板)

来源:互联网 发布:java list排序函数 编辑:程序博客网 时间:2024/06/08 14:55
NUMBER BASE CONVERSION
Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 5528Accepted: 2526

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.

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

862 2 abcdefghiz10 16 123456789012345678901234567890123456789016 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD235 23 333YMHOUE8JPLT7OX6K9FYCQ8A23 49 946B9AA02MI37E3D3MMJ4G7BL2F0549 61 1VbDkSIMJL3JjRgAdlUfcaWj61 5 dl9MDSWqwHjDnToKcsWE1S5 10 42104444441001414401221302402201233340311104212022133030

Sample Output

62 abcdefghiz2 1101110000010001011111001001011001111100100110001101001000110 123456789012345678901234567890123456789016 3A0C92075C0DBF3B8ACBC5F96CE3F0AD216 3A0C92075C0DBF3B8ACBC5F96CE3F0AD235 333YMHOUE8JPLT7OX6K9FYCQ8A35 333YMHOUE8JPLT7OX6K9FYCQ8A23 946B9AA02MI37E3D3MMJ4G7BL2F0523 946B9AA02MI37E3D3MMJ4G7BL2F0549 1VbDkSIMJL3JjRgAdlUfcaWj49 1VbDkSIMJL3JjRgAdlUfcaWj61 dl9MDSWqwHjDnToKcsWE1S61 dl9MDSWqwHjDnToKcsWE1S5 421044444410014144012213024022012333403111042120221330305 4210444444100141440122130240220123334031110421202213303010 1234567890123456789012345678901234567890

Source

Greater New York 2002


    

题目意思很简单,就是把m进制的一个大数转化为n进制的大数。

然后大数之间的进制转换算个模板吧,看了半天别人的博客才看懂,一开始只打算几个模板的,后来想想还是要理解,理解,理解。

抄的一个例子     源于某个博客,但是现在那个博客打不开。。。
假设要把一个二进制数1011,转换成一个三进制数。
1、用二进制数的第一位1去除以3,商为0,余数是1
2、由于前面一位的除法结果有余数,所以要把余数*(进制)+该为数字。对于本例,就是1(余数)*2(进制)+0(当前这位的数值)=2。除以3后,商0,余数是2
3、同2所述,2(余数)*2(进制)+1(当前这位的数值)=5。5除以3后,商1,余数2
4、同2所述,2(余数)*2(进制)+1(当前这位的数值)=5。5除以3后,商1,余数2
以上四步可以表示为 1011(2-based) / 3(要转换成的基数) = 0011(2-based 商).. 2(3-based 余数)
然后再让0011除以3,重复以上步骤,直到商为0。每次的余数的逆序就是以3为基的数。
1011/3 -->0011 ..2
0011/3 -->0001 ..0
0001/3 -->0000 ..1

所以,1011(2-based) --> 102(3-based)


看完是不是很清晰了。
如果还是不懂可以结合代码一起看一下。
下面就是放模板了。
代码

#include#include#include#include#include#define maxn 666using namespace std;string s;int a,b;int A[maxn],r[maxn],B[maxn];int Get_num(char ch){    if(ch>='0'&&ch<='9')        return ch-'0';    else if(ch>='A'&&ch<='Z')        return ch-'A'+10;    else        return ch-'a'+36;}void Get_s(){    A[0]=s.size();    //巧妙之处,本来没有这样写,写到一半才发现这样写更方便    for(int i=1;i<=A[0];i++)        A[i]=Get_num(s[i-1]);}char Get_char(int i){    if(i>=0&&i<=9)        return i+'0';    else if(i>=10&&i<=35)        return i-10+'A';    else        return i-36+'a';}void solve(){    while(A[0]>=1)     //循环,重复操作至商为0    {        int t=0,i;        for(i=1;i<=A[0];i++)        {            t=t*a+A[i];            B[++B[0]]=t/b;            t%=b;        }        r[++r[0]]=t;       //r存放最后的答案,即每次操作后的余数        B[0]=A[0];        for(i=1;i<=B[0]&&!B[i];i++);    //去b[]中开始‘0'        for(A[0]=0;i<=B[0];i++)        {            A[++A[0]]=B[i];            //跟新A[]中的值,即商的值        }        memset(B,0,sizeof B);       //清空    }}void print(){    cout<=1)    {        printf("%c",Get_char(r[r[0]--]));    }    cout<>a>>b>>s;        Get_s();        solve();        print();    }    return 0;}


原创粉丝点击