C++ 中的泰特拉奇数
在此,我们将看到如何使用 C++ 生成泰特拉奇数。泰特拉奇数类似于斐波那契数,但我们在这里通过添加四个前项来生成一项。假设我们想生成 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){
//function to generate n tetranacci numbers
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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP