Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.9 kB
3
Indexable
Never
import java.io.*;
import java.util.*;

class Solution {
    public String convertBase(String s, int a, int b) {
        long num = toDeci(s, a);
        String ans = fromDeci(b, num);
        return ans;
    }

    static int val(char c) {
        if (c >= ''0'' && c <= ''9'')
            return (int) c - ''0'';
        else
            return (int) c - ''A'' + 10;
    }

    static int toDeci(String str, int base) {

        int len = str.length();

        int power = 1;

        int num = 0;

        for (int i = len - 1; i >= 0; i--) {

            if (val(str.charAt(i)) >= base) {
                System.out.printf("Invalid Number");
                return -1;
            }

            num += val(str.charAt(i)) * power;

            power = power * base;
        }
        return num;
    }

    static char reVal(long num) {
        if (num >= 0 && num <= 9)
            return (char) (num + ''0'');
        else
            return (char) (num - 10 + ''A'');
    }

    static String fromDeci(int base, long inputNum) {

        String res = "";

        while (inputNum > 0) {

            res += reVal(inputNum % base);

            inputNum /= base;
        }

        res = reverse(res);

        return res;
    }

    static String reverse(String input) {
        char[] a = input.toCharArray();
        int l, r = a.length - 1;
        for (l = 0; l < r; l++, r--) {
            char temp = a[l];
            a[l] = a[r];
            a[r] = temp;
        }
        return String.valueOf(a);
    }
}

class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int x = sc.nextInt();
        int y = sc.nextInt();
        Solution obj = new Solution();
        System.out.println(obj.convertBase(s, x, y));
        sc.close();
    }
}