Tribonacci 单词(C++)
Tribonacci 单词是一个数字序列。类似于 Fibonacci 单词。Tribonacci 单词通过重复连接前面三个字符串来构建
T(n) = T(n - 1) + T(n - 2) + T(n - 3)
首先的几个字符串是 {1、12、1213},因此,下一个字符串将是 1213 + 12 + 1 = 1213121
算法
tribonacci_word(n): Begin first := 1, second := 12, third := 1213 print first, second, third for i in range 3 to n, do temp := third third := third + second + first print third first := second second := next done End
示例
#include<iostream> using namespace std; long tribonacci_word_gen(int n){ //function to generate n tetranacci words string first = "1"; string second = "12"; string third = "1213"; cout << first << "\n" << second << "\n" << third << "\n"; string tmp; for (int i = 3; i <= n; i++) { tmp = third; third += (second + first); cout << third <<endl; first = second; second = tmp; } } main(){ tribonacci_word_gen(6); }
输出
1 12 1213 1213121 1213121121312 121312112131212131211213 12131211213121213121121312131211213121213121
广告