HDU - 2563 - 统计有关问题

HDU - 2563 - 统计问题

统计问题

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5769    Accepted Submission(s): 3406


Problem Description
在一无限大的二维平面中,我们做如下假设:
1、  每次只能移动一格;
2、  不能向后走(假设你的目的地是“向上”,那么你可以向左走,可以向右走,也可以向上走,但是不可以向下走);
3、  走过的格子立即塌陷无法再走第二次;

求走n步不同的方案数(2种走法只要有一步不一样,即被认为是不同的方案)。
 

Input
首先给出一个正整数C,表示有C组测试数据
接下来的C行,每行包含一个整数n (n<=20),表示要走n步。
 

Output
请编程输出走n步的不同方案总数;
每组的输出占一行。
 

Sample Input
2 1 2
 

Sample Output
3 7
 

Author
yifenfei
 

Source
绍兴托普信息技术职业技术学院——第二届电脑文化节程序设计竞赛


也是找规律~~

a[n] = 2*a[n-1] + a[n-2];


AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int a[25] = {0, 3, 7};
void init()
{
	for(int i=3; i<=20; i++)
	{
		a[i] = 2*a[i-1]+a[i-2];
	}
}

int main()
{
	init();
	int c, n;
	scanf("%d", &c);
	while(c--)
	{
		scanf("%d", &n);
		printf("%d\n", a[n]);
	}
	return 0;
}