SGU112-a^b-b^a (BigNum)

2014-11-24 08:22:31 · 作者: · 浏览: 0
You are given natural numbers a and b. Find ab-ba.
Input
Input contains numbers a and b (1≤a,b≤100).
Output
Write answer to output.
Sample Input
2 3
Sample Output
-1
分析:
BigNum问题,这类问题很基础,虽然有模板,但是还是自己记住为好,以后碰到面试肯定不会让你带着模板去。
import java.math.BigInteger; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int a, b; while(cin.hasNext()) { a = cin.nextInt(); b = cin.nextInt(); System.out.println(BigInteger.valueOf(a).pow(b).add(BigInteger.valueOf(b).pow(a).negate())); } } }