• Codevs 2956 排队问题


    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
    ", f[n]);
    11     return 0;
    12 }
    13 // 这题居然是递推!!!!!! 
  • 相关阅读:
    软件的概念
    打开别人Android项目的方法
    软件工程
    数据预处理技术
    机器学习之监督学习
    C语言指针与二维数组
    徐涛政治押题
    求二叉树的宽度
    解决在某些IE浏览器下字符乱码的问题
    mac系统不能使用127.0.0.2的解决方案
  • 原文地址:https://www.cnblogs.com/suishiguang/p/6217501.html
Copyright © 2020-2023  润新知