问题描述
有一长度为N(1<=N<=10)的地板,给定两种不同瓷砖:一种长度为1,另一种长度为2,数目不限。要将这个长度为N的地板铺满,一共有多少种不同的铺法?
例如,长度为4的地面一共有如下5种铺法:
4=1+1+1+1
4=2+1+1
4=1+2+1
4=1+1+2
4=2+2
编程用递归的方法求解上述问题。
例如,长度为4的地面一共有如下5种铺法:
4=1+1+1+1
4=2+1+1
4=1+2+1
4=1+1+2
4=2+2
编程用递归的方法求解上述问题。
输入格式
只有一个数N,代表地板的长度
输出格式
输出一个数,代表所有不同的瓷砖铺放方法的总数
样例输入
4
样例输出
5
此题思路就是简单递归 选和不选
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #define ci cin.tie(0) #define ios ios::sync_with_stdio(false) #define fi first #define se second using namespace std; typedef long long LL; typedef pair<int, int> PII; const int N = 11; int n, ans; int a[N]; bool vis[N]; void dfs(int u) { if (u == n) { ans ++ ; return ; } if (u > n) return ; // 选1 u += 1; dfs(u); u -= 1; // 选2 u += 2; dfs(u); u -= 2; } int main() { ci;ios; cin >> n; dfs(0); cout << ans << endl; return 0; }