递归算法:
public int getSum(int n) { //实际上方法只执行了一次 内部 getSum(n - 1)多次执行;
if (n == 1){
return 1; //终止条件
}else {
return n + getSum(n - 1);
//1. 5 + 4 4 + 3 3 + 2 2 + 1(终止)
//2. 5 + 10 <--- 4 + 6 <--- 3 + 3 <--- 2 + 1(终止): 3 = 2 + 1...依次类推...
//假设 n = 5; n + n-1 ---> n + n-1 ---> n + n-1 ---> n + n-1;
}
}
相关文章
- 11-152021-09-06
- 11-152021-09-06
- 11-152021-09-06
- 11-152021-09-06
- 11-152021-09-06
- 11-152021-09-06
- 11-152021-09-06
- 11-152021-09-06
- 11-152021-09-06
- 11-152021-09-06 JMETER压力测试报错:JAVA.NET.BINDEXCEPTION: ADDRESS ALREADY IN USE: CONNECT