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 婆婆高寿祝福语大全简短