SGU 112:a^b-b^a

Jasonlin posted @ 2011年3月31日 22:17 in SGU with tags 数学 高精度 , 5108 阅读

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=112

题目大意:

      输入a,b输出a^b-b^a的值。(1<=a,b<=100)

解题思路:

      肯定是用高精度算法,可是比较懒阿,先用JAVA写一个,然后C++的以后写吧。

解题代码:

import java.math.*;
import java.io.*;
import java.util.*;
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).subtract(BigInteger.valueOf(b).pow(a)));
        }
    }
}

扩展知识:http://blog.csdn.net/soberman/archive/2009/03/10/3978074.aspx

Avatar_small
依云 说:
2011年3月31日 23:47

我来用 Python 写个——
try:
a, b = map(int, input('a b: ').strip().split())
print(a ** b - b ** a)
except:
print('input error')

Avatar_small
comathlish 说:
2011年4月01日 10:12

现在的高级语言是越来越强大啦~


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter