The number of decimal conversion

124. The number of decimal conversion

Write a program that can achieve a number of a band into another band.
There are 62 different digits {0-9, AZ, az}.
Input format
The first line input an integer representing the number of the next row.
Each row contains the next three digits, first binary input (decimal notation), then the binary output (decimal notation), and finally enter the number represented by the binary input, separated by a space between the numbers.
Binary input and binary output are in the range of 2-62.
(In decimal) A = 10, B = 11 , ..., Z = 35, a = 36, b = 37, ..., z = 61 (0-9 still represent 0-9).
Output Format
For each set of binary conversion, the output of the program constituted by the three lines.
The first line contains two numbers, the first input is hexadecimal (decimal notation), then the input number represented by the binary input.
The second line contains two numbers, the first output is binary (decimal notation), then the digital representation of the input binary output.
The third row is blank.
Within the same line numbers separated by a space.
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

1. High-precision conversion

Short division Qin Jiushao +
[Image dump outer link fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-wWd3ZlSg-1583898444912) ( p1.png)]
to use a binary Qin Jiushao converted into 10 system, e.g. ( 12345 ) 6 = ( ( ( ( ( 1 × 6 ) × 2 × 6 ) × 3 × 6 ) × 4 × 6 ) × 5 ) (12345)_6=(((((1×6)×2×6)×3×6)×4×6)×5) .
Short decimal divider 10 and then converted to the b-ary.
Require high precision.
Here Insert Picture Description

2. Short division

We do division A division in accordance with a number of short hex, each retains the remainder of the division is a short routine. E.g ( 413 ) 5 (413)_5 3 is converted to hexadecimal ( 11000 ) 3 (11000)_3 . Here Insert Picture Description
Less than ten lines of code to the core finished.

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
string sa,sb;
int a,b;
int A[N],B[N];
void solve(){
    sb.clear();
    int cnt=0,k=0,j=0;
    for(auto c : sa){
        if(c<='9'&&c>='0') A[cnt++]=c-'0';
        if(c<='Z'&&c>='A') A[cnt++]=c-'A'+10;
        if(c<='z'&&c>='a') A[cnt++]=c-'a'+36;
    }
    while(j<cnt){
        for(int i=j;i<cnt-1;++i){
            A[i+1]+=A[i]%b*a;
            A[i]/=b;
        }
        B[k++]=A[cnt-1]%b;
        A[cnt-1]/=b;
        if(j<cnt&&A[j]==0) ++j;
    }
    for(int i=k-1;i>=0;--i){
        sb+=(char)(B[k-i-1]<=9?B[k-i-1]+'0':(B[k-i-1]<=35?B[k-i-1]-10+'A':B[k-i-1]-36+'a'));
    }
    reverse(sb.begin(),sb.end());
    j=0;
    while(sb[j]=='0'&&j<k-1) ++j;
    cout<<a<<" "<<sa<<'\n';
    cout<<b<<" ";
    for(j;j<k;++j)
        cout<<sb[j];
    cout<<'\n'<<'\n';
}
int main(){
    int T;
    cin>>T;
    while(T--){
        cin>>a>>b;
        cin>>sa;
        solve();
    }
}
Published 96 original articles · won praise 11 · views 2251

Guess you like

Origin blog.csdn.net/weixin_43769146/article/details/104793354