fjnu 1872 Function Run Fun

fjnu 1872 Function Run Fun
http://acm.fjnu.edu.cn/show?problem_id=1872
如果直接用递归函数肯定会tle,有两种解决方案:
1)先用暴力法列出数值表,找规律,或用数学方法推导,直接找到递推式。
2)在程序开始先算出所有题目要求的数值范围,并保存,在主程序里直接调用就行了。
#include  < stdio.h >

int  main()
{
    
int a,b,c,w[21][21][21];
    
for(a=0;a<21;a++){
                      
for(b=0;b<21;b++)
                                       w[
0][a][b]=w[a][0][b]=w[a][b][0]=1;
    }

    
for(a=1;a<21;a++){
                      
for(b=1;b<21;b++)
                                       
for(c=1;c<21;c++){
                                                         
if(a<&& b<c)
                                                                w[a][b][c]
=w[a][b][c-1+ w[a][b-1][c-1- w[a][b-1][c];
                                                         
else
                                                                w[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];
                                       }

    }

     
while(scanf("%d %d %d",&a,&b,&c),a!=-1||b!=-1||c!=-1){
     
if(a <= 0 || b <= 0 || c <= 0)
            printf(
"w(%d, %d, %d) = %d\n",a,b,c,1);
        
else if(a > 20 || b > 20 || c > 20)
            printf(
"w(%d, %d, %d) = %d\n",a,b,c,w[20][20][20]);
        
else
            printf(
"w(%d, %d, %d) = %d\n",a,b,c,w[a][b][c]);

    }

    
return 0;
}

你可能感兴趣的:(fjnu 1872 Function Run Fun)