NUMBER BASE CONVERSION(进制转化)

来源:互联网 发布:javascript运算符 编辑:程序博客网 时间:2024/05/19 10:40

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

解题思路:

题目大意是给一个a进制的数,让你转化成b进制,给的数会很大。无非就是不断求余,用高精度求固然可以,但十分麻烦。让被除数上的每一位先加上之前的余数乘以进制数再去除以除数得出的最后余数即是求出的余数。比如5进制数1234转化成2进制:第一位是1,1%2 = 1,1 / 2 = 0,则该位变为0,下一位为2,加上之前的余数乘以进制数,即2 + 1 * 5 = 7,7 % 2 = 1,7 / 2 = 3,则第二位变为3,依次类推,求出余数后,对已经改变的该进制数重新进行求余直至为0。

AC代码:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 10000;char num[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";int main(){    int t, a, b, len;    char str[maxn], str_2[maxn];    scanf("%d", &t);    while(t--)    {        int k, res, j;        scanf("%d%d", &a, &b);        scanf("%s", str);        printf("%d %s\n", a, str);        len = strlen(str);        bool flag = true;        int n=10;        j = 0;        while(flag)        {            flag = false;            res = 0;            for(int i = 0; i < len; i++)            {                if(str[i] >= '0' && str[i] <= '9')                    k = str[i] - '0';                if(str[i] >= 'A' && str[i] <= 'Z')                    k = str[i] - 'A' + 10;                if(str[i] >= 'a' && str[i] <= 'z')                    k = str[i] - 'a' + 36;                k = res * a + k;   // 加上之前的余数乘以进制数                res = k % b;   // 取余                str[i] = num[k / b];                if(str[i] != '0')    // 判断是否全为0                    flag = true;            }            str_2[j++] = num[res];  // 存储余数        }        printf("%d ", b);        for(int i = j - 1; i >= 0; i--)  // 倒序输出余数            printf("%c", str_2[i]);        printf("\n\n");    }}


0 0
原创粉丝点击