题目描述 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;
}