首页 > 试题广场 >

幂运算

[编程题]幂运算
  • 热度指数:5099 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 128M,其他语言256M
  • 算法知识视频讲解
给定两个数R和n,输出R的n次方,其中0.0<R<99.999, 0<n<=25

输入描述:
多组测试用例,请参考例题的输入处理 输入每行一个浮点数 R 其中0.0 < R <99.999, 一个整数 n 其中0 < n <=25


输出描述:
输出R的n次方
示例1

输入

95.123 12 0.1 1

输出

548815620517731830194541.899025343415715973535967221869852721 0.1
运行 12ms
import java.io.IOException;
import java.math.BigDecimal;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] tmp = br.readLine().split(" ");
        if (tmp.length > 3){
            int b = Integer.parseInt(tmp[1]);
            int d = Integer.parseInt(tmp[3]);
            BigDecimal a = new BigDecimal(tmp[0]);
            BigDecimal c = new BigDecimal(tmp[2]);
            BigDecimal num1 = a;
            BigDecimal num2 = c;
            for (int x = 1; x < b; x++) {
                num1 = num1.multiply(a);
            }
            for (int x = 1; x < d; x++) {
                num2 = num2.multiply(c);
            }
            System.out.println(num1 + " " + num2);
        }
    }
}


编辑于 2021-05-23 22:32:03 回复(0)