一、案例
public class Recursive {
public int sumRec(int num){
if(num == 1){
return 1;
}else {
return num + sumRec(num -1);
}
}
}
class RecTest{
public static void main(String[] args) {
Recursive rec = new Recursive();
System.out.println(rec.sumRec(100)); // 5050
}
}
/*
已知一个数列:f(0)=1,f(1)=4,f(n+2)=2*f(n-1)+f(n)
其中n大于0整数,求f(10)
*/
public class Recursive {
public int f(int n) {
if (n == 0) {
return 1;
} else if (n == 1) {
return 4;
} else {
return 2 * f(n - 1) + f(n - 2);
}
}
}
class RecTest {
public static void main(String[] args) {
Recursive rec = new Recursive();
System.out.println(rec.f(10));
}
}
原文地址:https://www.cnblogs.com/hq82/p/12121429.html
时间: 2024-10-13 00:14:57