天天看點

【題解】poj1220 NUMBER BASE CONVERSION 高精度+進位計數制

題目連結

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

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

代碼實作參考了大佬題解,字元和整型轉換的那句很精髓,自己還去翻了翻ASCII碼……

然鵝還是WA了,DEBUG很久發現問題出在N的取值……測試樣例時,當N=1009、1010、1011、1012時,讀入的m會在第一個for循環處變為-48……玄學

(天知道我是怎麼想到去查這個地方)

#include<cstdio>
#include<cstring>
const int N=;
char s[N],ans[N];
int T,n,m,a[N],b[N];
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%s",&n,&m,s);
        int len=strlen(s),pos=;
        for(int i=len;i>=;--i)a[len--i]=s[i]-(s[i]<?:s[i]<?:);
        while(len)
        {
            for(int i=len;i;i--)
            {
                a[i-]+=a[i]%m*n;
                a[i]/=m;
            }
            b[pos++]=a[]%m;
            a[]/=m;
            while(len&&!a[len-])len--;
        }
        ans[pos]=NULL;
        for(int i=;i<pos;i++)ans[pos-i-]=b[i]+(b[i]<?:b[i]<?:);
        printf("%d %s\n%d %s\n\n",n,s,m,ans);
    }
    return ;
}
           

總結

玄學DEBUG