递归实现
// f(n)=f(n-1)+3f(n-2);f(0)=0;f(1)=1; n>=2 public static int fac(int k) { if (k == 0) { return 0; } if (k == 1) { return 1; } return fac(k - 1) + 3 * fac(k - 2); }
循环实现
public static void main(String[] args) { // f(n)=f(n-1)+3f(n-2);f(0)=0;f(1)=1; int a = 0, b = 1, c = 0; for (int i = 2; i <= 11; i++) { c = b + 3 * a; a = b; b = c; } System.out.println(c); }