gpt4 book ai didi

java - 编写一个函数,打印出 n^100 的每个数字

转载 作者:行者123 更新时间:2023-12-01 08:00:16 26 4
gpt4 key购买 nike

我被要求编写一个 Java 函数,它接受一个整数 n,并打印出值 n^100

我不知道如何解决这个问题。我知道通过传统方式它会随着 n 的增长而溢出。诸如 5.32 x 10^20 之类的答案是 Not Acceptable 。必须是每个数字。

例如:

public void byHundred(int n) {
result = //some computation that yields the string
System.out.println(result);
}

因此,byHundred(23) 打印出 "148861915063630393937915565865597542319871196538013686865769882092224332785393313521523901432773 46804233476592179447310859520222529876001"

最佳答案

您可以使用BigInteger以及类似的东西,

public static void byHundred(int n) {
BigInteger bi = BigInteger.valueOf(n);
String result = bi.pow(100).toString();
System.out.println(result);
}

public static void main(String[] args) {
byHundred(23);
}

输出为14886191506363039393791556586559754231987119653801368686576988209222433278539331352152390143277346804233476592179 447310859520222529876001(根据要求)。

关于java - 编写一个函数,打印出 n^100 的每个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25877286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com