NUMBER BASE CONVERSION(高精度)

来源:互联网 发布:商机助理怎么传淘宝 编辑:程序博客网 时间:2024/05/19 23:19

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

解题思路

进制转换,高精度的除法,怎么记录商,怎么记录余数,都是要注意的。

刚看到题目的时候感觉这么多字符转换,好麻烦,想想还是好方法最重要


AC代码

#include<stdio.h>#include<string.h>#define MAXNSIZE 1005char str[MAXNSIZE];int start[MAXNSIZE], ans[MAXNSIZE], rest[MAXNSIZE];int oldBase, newBase;                                            //原来的进制, 现在的进制int getNum(char ch)                                              //字符 转换 数值{    if(ch >= '0' && ch <= '9')  return ch - '0';    if(ch >= 'A' && ch <= 'Z')  return ch - 'A' + 10;    return ch - 'a' + 36;}char getChar(int i)                                             //数值 转换 字符{    if(i >= 0 && i <= 9)  return '0' + i;    if(i >= 10 && i <= 35)  return 'A' + i - 10;    return 'a' + i - 36;}void change()                                                  //字符串 转换成 整形数组{    start[0] = strlen(str);    for(int i = 1; i <= start[0]; i++)        start[i] = getNum(str[i-1]);}void solve()                                                   //rest[0],start[0],ans[0],记录各数组的长度                 {    memset(rest, 0, sizeof(rest));    int y, i, j;    while(start[0] >= 1)    {        y = 0;  i = 1;        ans[0] = start[0];        while(i <= start[0])        {            y = y * oldBase + start[i];                        //进制转换,边乘边除求余,ans[]记录商,余数存入rest[]数组中            ans[i++] = y / newBase;            y %= newBase;        }        rest[ ++rest[0] ] = y;                                 //rest[]是倒序的        i = 1;        while(i <= ans[0] && ans[i] == 0)                      //去除前导0            i++;        memset(start, 0, sizeof(start));        for(j = i; j <= ans[0]; j++)            start[ ++start[0] ] = ans[j];                      //start[]赋新值,ans[]中的商        memset(ans, 0, sizeof(ans));    }}void output(){    printf("%d %s\n", oldBase, str);    printf("%d ", newBase);    for(int i = rest[0]; i >= 1; i--)        printf("%c", getChar(rest[i]));    printf("\n\n");}int main(){    int T;    scanf("%d", &T);    while(T--)    {        scanf("%d %d %s", &oldBase, &newBase, str);        change();        solve();        output();    }    return 0;}


0 0
原创粉丝点击