C++递推

第一题、

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a[110]={0};
	int b[110]={0};
	int c[110]={0};
	a[1]=0;
	b[1]=1;
	c[1]=0;
	int n;
	cin>>n;
	for(int i=2;i<=n;i++)
	{
		a[i]=a[i-1]+c[i-1];
		b[i]=a[i];
		c[i]=b[i-1];
	}
	cout<<a[n]+b[n]+c[n]<<endl;
	return 0;
}

 第二题、

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a[20]={0};
	a[10]=1;
	for(int i=9;i>=1;i--)
	{
		a[i]=(a[i+1]+1)*2;
	}
	cout<<a[1]<<endl;
	return 0;
}

第三题、

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a[110]={0};
	int b[110]={0};
	a[1]=1;
	b[1]=a[1];
	int n;
	cin>>n;
	for(int i=2;i<=n;i++)
	{
		a[i]=a[i-1]+i;
		b[i]=b[i-1]+a[i];
	}
	cout<<b[n]<<endl;
	return 0;
}

第四题、

 

#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long a[1100]={0};
	a[1]=1;
	a[2]=2;
	int n;
	cin>>n;
	for(int i=3;i<=n;i++)
	{
		a[i]=a[i-1]*2+a[i-2];
	}
	cout<<a[n]<<endl;
	return 0;
}

第五题、

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a[100]={0};
	a[1]=1;
	a[2]=2;
	a[3]=4;
	for(int i=4;i<=30;i++)
	{
		a[i]=a[i-1]+a[i-2]+a[i-3];
	}
	while(true)
	{
		int n;
		cin>>n;
		if(n==0)
		{
			break;
		}
		else
		{
			cout<<a[n]<<endl;
		}
	}
	return 0;
}

 第六题、

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a[110]={0};
	a[1]=1;
	a[2]=1;
	int M,N;
	cin>>M>>N;
	for(int i=3;i<=N-M+1;i++)
	{
		a[i]=a[i-1]+a[i-2];
	}
	cout<<a[N-M+1]<<endl;
	return 0;
}

第七题、

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a[60]={0};
	a[1]=1;
	a[2]=2;
	a[3]=4;
	int n;
	cin>>n;
	for(int i=4;i<=n;i++)
	{
		a[i]=a[i-1]+a[i-2]+a[i-3];
	}
	cout<<a[n]<<endl;
	return 0;
}

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++中二叉树的三种遍历方式的递归实现和非递归实现: 1. 先序遍历 递归实现: ```c++ void preorder(TreeNode* root) { if (root == nullptr) { return; } cout << root->val << " "; preorder(root->left); preorder(root->right); } ``` 非递归实现: ```c++ void preorder(TreeNode* root) { if (root == nullptr) { return; } stack<TreeNode*> s; s.push(root); while (!s.empty()) { TreeNode* node = s.top(); s.pop(); cout << node->val << " "; if (node->right != nullptr) { s.push(node->right); } if (node->left != nullptr) { s.push(node->left); } } } ``` 2. 中序遍历 递归实现: ```c++ void inorder(TreeNode* root) { if (root == nullptr) { return; } inorder(root->left); cout << root->val << " "; inorder(root->right); } ``` 非递归实现: ```c++ void inorder(TreeNode* root) { if (root == nullptr) { return; } stack<TreeNode*> s; TreeNode* node = root; while (node != nullptr || !s.empty()) { while (node != nullptr) { s.push(node); node = node->left; } node = s.top(); s.pop(); cout << node->val << " "; node = node->right; } } ``` 3. 后序遍历 递归实现: ```c++ void postorder(TreeNode* root) { if (root == nullptr) { return; } postorder(root->left); postorder(root->right); cout << root->val << " "; } ``` 非递归实现: ```c++ void postorder(TreeNode* root) { if (root == nullptr) { return; } stack<TreeNode*> s; TreeNode* node = root; TreeNode* last = nullptr; while (node != nullptr || !s.empty()) { while (node != nullptr) { s.push(node); node = node->left; } node = s.top(); if (node->right == nullptr || node->right == last) { cout << node->val << " "; s.pop(); last = node; node = nullptr; } else { node = node->right; } } } --相关问题--:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值