该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

java">import java.math.BigInteger;

public class Graham {

public BigInteger graham(int base, int arrow, BigInteger power) {

if (base < 1)

throw new IllegalArgumentException("参数base必须大于等于1。");

if (arrow < 1)

throw new IllegalArgumentException("参数arrow必须大于等于1。");

if (power == null)

throw new IllegalArgumentException("参数power不能为null。");

if (power.compareTo(BigInteger.ONE) < 0)

throw new IllegalArgumentException("参数power必须大于等于1。");

if (base == 1)

return BigInteger.ONE;

if (arrow == 1) {

return power(base, power);

} else {

return graham(base, arrow - 1, graham(base, arrow - 1, power));

}

}

public BigInteger power(int base, BigInteger power) {

if (power.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0)

return BigInteger.valueOf(base).pow(power.intValue());

return (BigInteger.valueOf(base).pow(Integer.MAX_VALUE)).multiply(power(base,

power.subtract(BigInteger.valueOf(Integer.MAX_VALUE))));

}

public static void main(String[] args) {

Graham self = new Graham();

System.out.print("graham(2, 2, 2): ");

System.out.println(self.graham(2, 2, BigInteger.valueOf(2)));

System.out.print("graham(2, 3, 2): ");

System.out.println(self.graham(2, 3, BigInteger.valueOf(2)));

System.out.print("graham(2, 3, 3): ");

System.out.println(self.graham(2, 3, BigInteger.valueOf(3)));

System.out.print("graham(3, 3, 3): ");

System.out.println(self.graham(3, 3, BigInteger.valueOf(3)));

}

}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐