Description 给出一个数列的递推公式,希望你能计算出该数列的第N个数。递推公式如下:
F(n)=F(n-1)+F(n-2)-F(n-3). 其中,F(1)=2, F(2)=3, F(3)=5.
很熟悉吧,可它貌似真的不是斐波那契数列呢,你能计算出来吗?
Input 输入只有一个正整数N(N>=4).
Output 输出只有一个整数F(N).
Sample Input 5 Output 8
import java.util.Scanner; public class Main { public static int f(int n) { int y; if (n == 1) y = 2; else if (n == 2) y = 3; else if (n == 3) y = 5; else y = f(n - 1) + f(n - 2) - f(n - 3); return y; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner reader = new Scanner(System.in); int n; n = reader.nextInt(); System.out.print(f(n)); } }