Created
May 2, 2018 03:20
-
-
Save IanSmith123/f43277e3eb77148304d08549bb790718 to your computer and use it in GitHub Desktop.
裴波那契数列
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 新裴波那契数列 | |
/* | |
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