Pages

Monday 1 October 2012

UVA - 355 - The Bases Are Loaded


import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        StringBuffer sb = new StringBuffer("");
        while (s.hasNext()) {
            int basefrom = s.nextInt();
            int baseto = s.nextInt();
            String temp = s.next();
            try {
                BigInteger sum = new BigInteger(temp, basefrom);
                String ans = sum.toString(baseto);
                sb.append(temp).append(" base ").append(basefrom).append(" = ").append(ans.toUpperCase()).append(" base ").append(baseto).append("\n");
            } catch (Exception e) {
                sb.append(temp).append(" is an illegal base ").append(basefrom).append(" number\n");
            }
        }
        System.out.print(sb);
    }
}

No comments:

Post a Comment