假设n的值大于0。
一:源程序:
public class RecursionTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
RecursionTest rt = new RecursionTest();
int x = 6;
int y = 2;
long result = rt.getPower(x, y);
System.out.println("The result of "+x+"'s "+y+" times power = "+result);
}
private long getPower(int x, int y){
if(y == 0) return 1;
if(y == 1) return x;
return x*getPower(x, y-1);//36
// return x*getPower(x, --y);//36
// return x*getPower(x, y--);//Exception in thread "main" java.lang.StackOverflowError:当应用程序递归太深而发生堆栈溢出时,抛出该错误。
}
}
求幂次方的递归实现方法1:
private long getPower(int x, int y){if(y == 0) return 1;
if(y == 1) return x;
return x*getPower(x, y-1);
}
方法2:
private long getPower(int x, int y){if(y == 0) return 1;
if(y == 1) return x;
return x*getPower(x, --y);
}
这两种方法应该是一样的,只不过写法不同。
至于下面的方法3:
private long getPower(int x, int y){
if(y == 0) return 1;
if(y == 1) return x;
return x*getPower(x, y--);
}
这种方法会抛出异常:Exception in thread "main" java.lang.StackOverflowError:当应用程序递归太深而发生堆栈溢出时,抛出该错误。
至于到底递归有多深呢?可以用下面的程序查看一下:
public class RecursionTest {
/**
* @param args
*/
static long num = 0;
public static void main(String[] args) {
// TODO Auto-generated method stub
RecursionTest rt = new RecursionTest();
int x = 6;
int y = 2;
long result = rt.getPower(x, y);
System.out.println("The result of "+x+"'s "+y+" times power = "+result);
}
private long getPower(int x, int y){
num ++;
System.out.println("执行方法getPower的次数:"+num);
if(y == 0) return 1;
if(y == 1) return x;
return x*getPower(x, y--);
}
}
运行结果:
.......
执行方法getPower的次数:3393
执行方法getPower的次数:3394
执行方法getPower的次数:3395
执行方法getPower的次数:3396
执行方法getPower的次数:3397
执行方法getPower的次数:3398Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.ext.DoubleByteEncoder.encodeArrayLoop(Unknown Source)
at sun.nio.cs.ext.DoubleByteEncoder.encodeLoop(Unknown Source)
at java.nio.charset.CharsetEncoder.encode(Unknown Source)
at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.BufferedWriter.flushBuffer(Unknown Source)
at java.io.PrintStream.newLine(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at one.RecursionTest.getPower(RecursionTest.java:24)
at one.RecursionTest.getPower(RecursionTest.java:26)
at one.RecursionTest.getPower(RecursionTest.java:26)
at one.RecursionTest.getPower(RecursionTest.java:26)
........
方法4:据说可以节省运算时间和一半的运算量,我是在网上搜到的。
源程序:
public class RecursionTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
RecursionTest rt = new RecursionTest();
int x = 6;
int y = 2;
long result = rt.getPower(x, y);
System.out.println("The result of "+x+"'s "+y+" times power = "+result);
}
private long getPower(int n, int m){
assert m >= 0;
if(m == 0) return 1;
if(m == 1) return n;
long temp = getPower(n,m/2);
return m%2 == 0? temp * temp: temp * temp * n;
}
}
方法4的参考出处:http://shenyu.iteye.com/blog/192063