1-n之间所有自然数的和,
1-n之间所有自然数的乘积,
已知f(0)=1,f(1)=4,f(n+2)=2*f(n+1)+f(n),求f(10)`
public class YiDaoYiBaiHe {
public static void main(String[] args) {
// TODO Auto-generated method stub
YiDaoYiBaiHe c=new YiDaoYiBaiHe();
System.out.println(c.sum(100));
System.out.println(c.sum1(4));
System.out.println(c.f(10));
}
//1-num之间所有自然数和
public int sum(int num){
if(num==1){
return 1;
}else{
return num+sum(num-1);
}
}
//1-num之间所有自然数的乘积
public int sum1(int n) {
if(n==1) {
return 1;
}else {
return n*sum1(n-1);
}
}
//已知f(0)=1,f(1)=4,f(n+2)=2*f(n+1)+f(n),求f(10)
public int f(int n) {
if(n==0) {
return 1;
}else if(n==1) {
return 4;
}else {
//return f(n+2)-2*f(n+1):栈溢出报错
return 2*f(n-1)+f(n-2);
}
}
}