UVA – 10551 – Basic Remains


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

public class Main {
	public static void main(String[] args) throws IOException {
		 Scanner in = new Scanner(System.in);
		 int r=-1;
			while ((r = in.nextInt()) != 0) {
			BigInteger a = new BigInteger(in.next(), r);
			BigInteger b = new BigInteger(in.next(), r);
			System.out.println(toBase(a.mod(b), new BigInteger(""+ r)));
		}
	}
	
	private static String toBase(BigInteger bigInteger, BigInteger r) {
		BigInteger value = bigInteger;
		char[] modes= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); 
		if (value.equals(BigInteger.ZERO))
			return "0";
		String result = "";
		while (value.compareTo(BigInteger.ZERO) ==1) {
			BigInteger mod = value.mod(r);
			value = value.subtract(mod);
			if (value.compareTo(BigInteger.ZERO) ==1) value = value.divide(r);
			result = modes[mod.intValue()] + result;
		}

		return result;
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: