1 public class Power{ 2 public long power(int a, int b){ 3 if (b == 0){ 4 return 1; 5 } 6 if (a == 0) { 7 return 0; 8 } 9 10 long half = power(a,b/2);11 if(b % 2 == 1){12 return half * half * a;13 }14 return half * half;15 }16 }
本文共 398 字,大约阅读时间需要 1 分钟。
1 public class Power{ 2 public long power(int a, int b){ 3 if (b == 0){ 4 return 1; 5 } 6 if (a == 0) { 7 return 0; 8 } 9 10 long half = power(a,b/2);11 if(b % 2 == 1){12 return half * half * a;13 }14 return half * half;15 }16 }
转载于:https://www.cnblogs.com/xiaofenyaonuli/p/8705641.html