Codevs 2956 排队问题

2956排队问题解析
探讨了2956排队问题,要求N个学生按2人或3人一组进行分组的所有可能方式。提供了两种解决方案,一种是深度优先搜索(DFS)方法,另一种是使用动态规划(DP)的递推方法。

2956 排队问题

 时间限制: 1 s    空间限制: 32000 KB    题目等级 : 黄金 Gold

题目描述 Description

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

 

输入描述 Input Description

一个数,N

输出描述 Output Description

一个数,即答案。

样例输入 Sample Input

6

样例输出 Sample Output

2

数据范围及提示 Data Size & Hint

N<=150

50分TLE代码存档:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 int n,ans;
 6 void dfs(int x)
 7 {
 8     if(x==0){
 9         ans++;return ; 
10     }
11     else if(x<2&&x!=0) return;
12     dfs(x-2);
13     dfs(x-3);
14 }
15 int main()
16 {
17     scanf("%d",&n);
18     dfs(n);
19     printf("%d",ans);
20     return 0;
21 }

正解:

 1 #include<cstdio>
 2 #define ll long long 
 3 using namespace std;
 4 ll f[155];
 5 int main() {
 6     int n;
 7     scanf("%d", &n);
 8     f[1]=0; f[2]=f[3]=1;
 9     for(int i=4;i<=n;i++) f[i]=f[i-2]+f[i-3];
10     printf("%lld\n", f[n]);
11     return 0;
12 }
13 // 这题居然是递推!!!!!! 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值