Skip to content

Instantly share code, notes, and snippets.

@IanSmith123
Created May 2, 2018 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IanSmith123/f43277e3eb77148304d08549bb790718 to your computer and use it in GitHub Desktop.
Save IanSmith123/f43277e3eb77148304d08549bb790718 to your computer and use it in GitHub Desktop.
裴波那契数列
// 新裴波那契数列
/*
Time Limit: 1000ms
Memory Limit: 64M
Description:
斐波那契数列指, F(n) = F(n-1) + F(n-2),F(1)=F(2)=1
我们定义新斐波那契数列为F(n)=2*F(n-1)+3*F(n-2)+5*F(n-3),F(1)=F(2)=F(3)=1
Input:
输入多组数据
每组数据仅有一行,为所求新斐波那契数列的下标n, n<=40
Output:
每组数据输出一行,为所求新斐波那契数列的值
Sample Input:
4
Sample Output:
10
*/
#include<iostream>
using namespace std;
#define MAX 40
int result[MAX] = {0};
long long fun(int n){
result[0] = 1;
result[1] = 1;
result[2] = 1;
if(n<=3){
return 1;
}
else{
for(int i=3; i<n; i++) {
result[i] = 2*result[i-1]+3*result[i-2]+5*result[i-3];
}
return result[n-1];
}
}
int main() {
int input;
cin >> input;
while(input >0){
cout<<fun(input)<<endl;
cin>>input;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment