C语言训练-1689-斐波那契?

Problem 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
Sample Output
8

#include<bits/stdc++.h>
int f(int x)
{
	if(x==1)
	{
		return 2;
	}
	if(x==2)
	{
		return 3;
	}
	if(x==3)
	{
		return 5;
	}
	else 
	{
		return f(x-1)+f(x-2)-f(x-3);
	}
}
int main(){
	int n;
	scanf("%d",&n);
	printf("%d",f(n));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43450493/article/details/83585479