现在的位置: 首页 > 综合 > 正文

poj1579 递推

2013年05月06日 ⁄ 综合 ⁄ 共 617字 ⁄ 字号 评论关闭

递推的简单题目,根据题目的递归函数模拟一下递推过程即可。

#include <iostream>
using namespace std;

const int size = 21;
int map[size][size][size];

void makeMap()
{
	int a, b, c;

	for(a = 0; a < size; a++)
		for(b = 0; b < size; b++)
			for(c = 0; c < size; c++)
			{
				if(a <= 0 || b <=0 || c <= 0)
					map[a][b][c] = 1;
				else if(a < b && b < c)
					map[a][b][c] = map[a][b][c-1] + map[a][b-1][c-1] - map[a][b-1][c];
				else 
					map[a][b][c] = map[a-1][b][c] + map[a-1][b-1][c] + map[a-1][b][c-1] - map[a-1][b-1][c-1];
			}
}

int main()
{
	int a, b, c, ans;
    
	makeMap();
	while(scanf("%d %d %d", &a, &b, &c) == 3)
	{
		if(a == -1 && b == -1 && c == -1) 
			break;
        
		if(a < 0 || b < 0 || c < 0)
			ans = 1;
		else if(a > 20 || b > 20 || c > 20)
			ans = map[20][20][20];
		else 
			ans = map[a][b][c];
        printf("w(%d, %d, %d) = %d\n", a, b, c, ans);
	}
	return 0;
}

 

抱歉!评论已关闭.