C ++中的Tetranacci数字
在这里,我们将看到如何使用C++生成Tetranacci数字。Tetranacci数与Fibonacci数类似,但是在这里我们通过添加四个先前的项来生成一个项。假设我们要生成T(n),则公式将如下所示-
T(n) = T(n - 1) + T(n - 2) + T(n - 3) + T(n - 4)
前几个数字为{0,1,1,2}
算法
tetranacci(n):
Begin
first := 0, second := 1, third := 1, fourth := 2
print first, second, third, fourth
for i in range n – 4, do
next := first + second + third + fourth
print next
first := second
second := third
third := fourth
fourth := next
done
End示例
#include<iostream>
using namespace std;
long tetranacci_gen(int n){
//函数生成n个正弦编号
int first = 0, second = 1, third = 1, fourth = 2;
cout << first << " " << second << " " << third << " " << fourth << " ";
for(int i = 0; i < n - 4; i++){
int next = first + second + third + fourth;
cout << next << " ";
first = second;
second = third;
third = fourth;
fourth = next;
}
}
main(){
tetranacci_gen(15);
}输出结果
0 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短