Description
On the Day of the Flag of Russia a shop-owner decided to decorate the show-window of his shop with textile stripes of white, blue and red colors. He wants to satisfy the following conditions:
- Stripes of the same color cannot be placed next to each other.
- A blue stripe must always be placed between a white and a red or between a red and a white one.
Determine the number of the ways to fulfill his wish.
Example.
For
N
= 3 result is following:
Input
N, the number of the stripes, 1 ≤
N
≤ 45.
Output
M, the number of the ways to decorate the shop-window.
Sample Input
input | output |
---|---|
3 | 4 |
#include <stdio.h>
#include <string.h>
long long m[50][2];
// 0 白色
// 1 红色
//m[i][j]代表使用i张旗以j=0白色结尾j=1以红色结尾
int main()
{
int n;
scanf("%d",&n);
m[0][0]=m[0][1]=0; //因为下面有i-2 所以添上m[0][0]和m[0][1],他们代表的是用0个
m[1][0]=m[1][1]=1;
for(int i=2; i<=n; i++)
{
m[i][0]=m[i-1][1]+m[i-2][1]; //第i块是白色的方案总数是第i-1块是红色,加上,i-1是蓝色,第i-2必须是红色的方案数量之和
m[i][1]=m[i-1][0]+m[i-2][0]; //第i块是红色的方案总数是第i-1块是白色,加上,i-1是蓝色,第i-2必须是白色的方案数量之和
}
printf("%lld\n",m[n][0]+m[n][1]);
return 0;
}
#include <string.h>
long long m[50][2];
// 0 白色
// 1 红色
//m[i][j]代表使用i张旗以j=0白色结尾j=1以红色结尾
int main()
{
int n;
scanf("%d",&n);
m[0][0]=m[0][1]=0; //因为下面有i-2 所以添上m[0][0]和m[0][1],他们代表的是用0个
m[1][0]=m[1][1]=1;
for(int i=2; i<=n; i++)
{
m[i][0]=m[i-1][1]+m[i-2][1]; //第i块是白色的方案总数是第i-1块是红色,加上,i-1是蓝色,第i-2必须是红色的方案数量之和
m[i][1]=m[i-1][0]+m[i-2][0]; //第i块是红色的方案总数是第i-1块是白色,加上,i-1是蓝色,第i-2必须是白色的方案数量之和
}
printf("%lld\n",m[n][0]+m[n][1]);
return 0;
}