1.链接地址
https://vjudge.net/problem/POJ-1953
2.问题描述
Given a positive integer n, determine the number of different chanting patterns of this length, i.e., determine the number of n-bit sequences that contain no adjacent 1's. For example, for n = 3 the answer is 5 (sequences 000, 001, 010, 100, 101 are acceptable while 011, 110, 111 are not).
输入样例
2 3 1
输出样例
Scenario #1: 5 Scenario #2: 2
3.解题思路
假设有n个空要填,队列称为f(n),第一个填了1,第二个只能填0,后面就是f(n-2)的排列;假设第一个填了0,后面就可以是f(n-1)的排列,所以这其实是一个斐波那契数列
4.算法实现源代码
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 50; long long int arry[maxn]; void init() { memset(arry,0,sizeof(arry)); arry[1]=2; arry[2]=3; for(int i=3;i<50;i++) { arry[i]=arry[i-1]+arry[i-2]; } } int main() { init(); int n; scanf("%d",&n); int ans,temp=1; for(int i=0;i<n;i++) { scanf("%d",&ans); printf("Scenario #%d:\n%lld\n\n",temp,arry[ans]); temp++; } }