不使用递归
static int fun(int x1, int x2, int frequency) { int temp = 0; if (frequency <= 0) return 0; else if (frequency == 1) return x1; else if (frequency == 2) return x2; else { for (int i = frequency - 2; i >= 1; i--) { temp = x1 + x2; x1 = x2; x2 = temp; } return temp; } }
递归
static int fun(int i) { if (i <= 0) { return 0; } else if (i > 0 && i <= 2) { return 1; } else { return fun(i - 2) + fun(i - 1); } }