|
发表于 2022-5-14 23:12:21
|
显示全部楼层
本楼为最佳答案
参考代码:
- import java.util.Scanner;
- public class PowTest {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.print("x:");
- Integer x = scan.nextInt();
- System.out.print("y:");
- Integer y = scan.nextInt();
- Integer value = pow(x, y);
- System.out.println(value);
- }
- public static Integer pow(Integer x, Integer y) {
- if (y == 0) {
- return 1;
- }
- if (y == 1) {
- return x;
- }
- if (y % 2 == 0) {
- y = pow(x, y / 2);
- return y * y;
- }
- y = pow(x, (y - 1) / 2);
- return pow(x, 1) * y * y;
- }
- }
复制代码 |
|