Pages

Friday 21 September 2012

UVA - 748 - Exponentiation


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

public class Main {

    public static void main(String[] args) throws IOException {
        Scanner s = new Scanner(System.in);
        StringBuffer sb = new StringBuffer("");
        String m = "";
        int x = 0;
        while (s.hasNext()) {
            String str = s.next();
            int ex = s.nextInt();
            BigDecimal bd = new BigDecimal(str);
            BigDecimal result=bd.pow(ex);
            result=zeros(result);
            String temp = result.toPlainString();
            if (temp.charAt(0) == '0') {
                temp = temp.substring(1);
            }
            sb.append(temp).append("\n");
        }
        System.out.print(sb);
    }

     static BigDecimal zeros(BigDecimal n) {
        try {
            while (true) {
                n = n.setScale(n.scale() - 1);
            }
        } catch (ArithmeticException e) {
           
        }
        return n;
    }
}

No comments:

Post a Comment