
The Motzkin number series starts with 1, 1, 4, 9, etc.., We can get the generalised nth term with the sequence. The Motzkin number sequence is as follows.
a0 = 1
a1 = 1
a2 = 4
立即学习“C++免费学习笔记(深入)”;
a3 = 9
an = ((2 * n + 1)/ n + 2) * M(n-1) +((3 * n - 3)/ n + 2) * M(n - 2)
功能说明:1 会员可申请开店功能2 购买在线扣除金额3 冲值卡自动生成4 支持2级分类5 数据库压缩和备份6 会员分5个级别7 商品带讨论8 自带融合论坛,可关闭打开9 密码找回功能10 新闻``滚动新闻``帮助中心11 后台设置前台会员的上传权限12 可关闭/打开商店13 会员自助发布商品功能14 用户问题咨询管理
0
Initialise the number n.
Iterate till n.
Update the previous two numbers
Return the last number.
Following is the implementation of the above algorithm in C++
#include <bits/stdc++.h>
using namespace std;
int getNthTerm(int n) {
if(n == 0 || n == 1) {
return 1;
}
int a = 1, b = 1;
for(int i = 2; i <= n; ++i) {
int c = ((2 * i + 1) * b + (3 * i - 3) * a) / (i + 2);
a = b;
b = c;
}
return b;
}
int main() {
int n = 5;
cout << getNthTerm(n) << endl;
return 0;
}如果您运行上述代码,您将得到以下结果。
21
以上就是在C++中,Motzkin数的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号