zoj 1168 || poj 1579 Function Run Fun

递归 = =。。

 

题目的意思是,给你递归式,算。。。

 

开始没仔细看题,做到后来,瞟见一句话

 

The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15, b = 15, c = 15), the program takes hours to run because of the massive recursion.

 

= =。好吧。怪我。没好好看题。本来想找规律的,找到一点规律不过不够用,然后就想着,打表吧。

 

一会一看 = =。。好做啊,每个数字只跟前面的数字有关系,好吧,打表吧 = =。。

 

超过20全按20算了,多好,果断打表过了。如果把这个数组存一下,再弄个新程序,直接打表,估计会更快~~

 

#include <cstdio> #include <cstdlib> #include <iostream> #include <string> using namespace std; int num[30][30][30]; int w(int a,int b,int c) { if( a <= 0 || b <= 0 || c <= 0 ) return 1; if( a < b && b < c ) return num[a][b][c-1] + num[a][b-1][c-1] - num[a][b-1][c]; return num[a-1][b][c] + num[a-1][b-1][c] + num[a-1][b][c-1] - num[a-1][b-1][c-1]; } int main() { int a,b,c; for(a=0; a<=20; a++) for(b=0; b<=20; b++) for(c=0; c<=20; c++) num[a][b][c] = w(a,b,c) ; while( scanf("%d%d%d",&a,&b,&c) != EOF ) { if( a == b && b == c && c == -1 ) break; if( a <= 0 || b <= 0 || c <= 0 ) { printf("w(%d, %d, %d) = 1/n",a,b,c); continue; } if( a > 20 || b > 20 || c > 20 ) { printf("w(%d, %d, %d) = %d/n",a,b,c,num[20][20][20]); continue; } printf("w(%d, %d, %d) = %d/n",a,b,c,num[a][b][c]); } return 0; }  

你可能感兴趣的:(zoj 1168 || poj 1579 Function Run Fun)