zoukankan      html  css  js  c++  java
  • cogs 1396. wwww

    1396. wwww

    ☆   输入文件:wwww.in   输出文件:wwww.out   简单对比
    时间限制:1 s   内存限制:256 MB

    【题目描述】

     

    对于一个递归函数w(a,b,c)

    如果 a<=0 或者 b<=0 或者 c<=0 就返回1. 否则

    如果 a>20 或者 b>20 或者 c>20 就返回w(20,20,20) 否则

    如果 a<b 并且 b<c 就返回w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c) 否则

    返回w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1)

    这是个简单的递归函数,但实现起来可能会有些问题。某些a、b、c的值会使函数运行时间无法忍受。

     

    【输入格式】

    输入文件有n+1(0<=n<=10000)行,第i(1<=i<=n)行有三个整数ai,bi,ci(保证在pascal的longint 或 C/C++的long范围内)。

    第n+1行必定是-1 -1 -1(同时保证其他行不会是-1 -1 -1)。

    【输出格式】

    输出文件有n行,第i行输出w(ai,bi,ci)的值(保证在pascal的int64 或 C/C++的long long范围内)。

    【样例输入】

    1 1 1
    2 2 2
    -1 -1 -1

    【样例输出】

    2
    4

    【提示】

    输出的第一行是w(1,1,1),输出的第2行是w(2,2,2)。

    【来源】

    题目提供者邮箱:darkgodz@qq.com

    (感谢题目提供者对COGS评测系统的支持)

    思路:题目已经提示的很明显了,别忘了加上记忆化。

    #include<cstdio>  
    #include<cstring>  
    #include<iostream>
    #include<algorithm>
    using namespace std;  
    int a,b,c;
    int f[30][30][30];  
    int w(int a,int b,int c){  
          if(a<=0||b<=0||c<=0){ return 1; }  
          if(a>20||b>20||c>20){ return(w(20,20,20)); }  
          if(f[a][b][c]>0)    return(f[a][b][c]);
          if(a<b && b<c){ f[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c); return(f[a][b][c]); }  
          f[a][b][c]=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);  
          return(f[a][b][c]);
    }  
    int main(){
        freopen("wwww.in","r",stdin);  
        freopen("wwww.out","w",stdout);    
          while(scanf("%d%d%d",&a,&b,&c)){  
            if(a==-1&&b==-1&&c==-1)    return 0;
            printf("%d
    ",w(a,b,c));  
          }    
    }  
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    css dropdown menu
    Understanding Delegated JavaScript Events
    AngularJS-Learning ui-router angular-transitions
    javascript sorting/ v8 sorting
    Sublime Text 3 新手上路:必要的安裝、設定與基本使用教學
    express with bower in websotrm
    angularjs transitions
    create a nodejs npm package
    nodejs module/require
    stylus , another css processor
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/7732064.html
Copyright © 2011-2022 走看看