标签:
对于一个递归函数w(a,b,c)
如果a<=0 or b<=0 or c<=0就返回值1.
如果a>20 or b>20 or 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均为15时,调用的次数将非常的多。你要想个办法才行.
会有若干行.
并以-1,-1,-1结束.
输出若干行
huyichen
记得开 unsigned long long !!! 血的教训!!
CODE:
#include <iostream> #include <cstdio> #include <cstring> #define REP(i, s, n) for(int i = s; i <= n; i ++) #define REP_(i, s, n) for(int i = n; i >= s; i --) #define MAX_N 20 + 10 using namespace std; int main(){ int a, b, c; unsigned long long F[MAX_N][MAX_N][MAX_N]; while(scanf("%d%d%d", &a, &b, &c) != EOF){ if(a == -1 && b == -1 && c == -1) break; memset(F, 0, sizeof(F)); REP(i, 0, 20) REP(j, 0, 20) REP(k, 0, 20) if(i == 0 || j == 0 || k == 0) F[i][j][k] = 1; REP(i, 1, 20) REP(j, 1, 20) REP(k, 1, 20){ if(i < j && j < k) F[i][j][k] = F[i][j][k - 1] + F[i][j - 1][k - 1] - F[i][j - 1][k]; else F[i][j][k] = F[i - 1][j][k] + F[i - 1][j - 1][k] + F[i - 1][j][k - 1] - F[i - 1][j - 1][k - 1]; } if(a <= 0 || b <= 0 || c <= 0) printf("w(%d, %d, %d) = 1\n", a, b, c); else if(a > 20 || b > 20 || c > 20) printf("w(%d, %d, %d) = %llu\n", a, b, c, F[20][20][20]); else printf("w(%d, %d, %d) = %llu\n", a, b, c, F[a][b][c]); } return 0; }
VJ 1080 Function(Function(F...
标签:
原文地址:http://www.cnblogs.com/ALXPCUN/p/4530385.html