2956 排队问题

题目描述 Description

有N个学生去食堂,可教官规定:必须2人或3人组成一组,求有多少种不同分组的方法。

 

输入描述 Input Description

一个数,N

输出描述 Output Description

一个数,即答案。

样例输入 Sample Input

6

样例输出 Sample Output

2

数据范围及提示 Data Size & Hint

N<=150


思路:a[n] = a[n-2] + a[n-3];

#include<iostream>
using namespace std;
int main()
{
	long long a[200] = {0,0,1,1};
	int n,i;
	cin>>n;
	for (i=4; i<=n; i++)
	{
		a[i] = a[i-2]+a[i-3];
	}
	cout<<a[n];
	return 0;
}

递归

#include<iostream>
using namespace std;
int n;
long long cnt=0,f[200]={0};
void dfs(int x)
{
	long long s=cnt;
	if (f[x])
	{
		cnt+=f[x];
		return ;
	}
	if (x<0)
	 return ;
	if (x == 0)
	{
		cnt++;
		return ;
	}
	dfs(x-2);
	dfs(x-3);
	f[x] = cnt - s;
}
int main()
{
	cin>>n;
	dfs(n);
	cout<<cnt;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值