hdoj 2046 骨牌铺方格

Description

在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数. 例如n=3时,为2× 3方格,骨牌的铺放方案有三种,如下图:

这里写图片描述

Input

输入数据由多行组成,每行包含一个整数n,表示该测试实例的长方形方格的规格是2×n (0< n<=50)。

Output

对于每个测试实例,请输出铺放方案的总数,每个实例的输出占一行。

Sample Input

1
3
2

Sample Output

1
3
2

Hint

此题用long long 类型

题解

设规格为2*n的时候有f(n)种方法,则f(n)等于f(n-2)+f(n-1)
f(n-2):最后两个空横着放有一种方法,前面n-2个空有f(n-2)种方法
f(n-1):最后一个空竖着放有一种方法,前面n-1个空有f(n-1)种方法

CODE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <time.h>
using namespace std;
typedef long long LL;
#define MAXN 50+10
int main()
{
    LL a[MAXN];
    a[1]=1;a[2]=2;
    for(int i=3;i<MAXN;i++)
            a[i]=a[i-2]+a[i-1];
    int n;
    while(cin >> n)
        cout << a[n] << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/AC__GO/article/details/81177853