hdu1297:Children’s Queue(大数递推)

http://acm.hdu.edu.cn/showproblem.php?pid=1297

Problem Description

There are many students in PHT School. One day, the headmaster whose name is PigHeader wanted all students stand in a line. He prescribed that girl can not be in single. In other words, either no girl in the queue or more than one girl stands side by side. The case n=4 (n is the number of children) is like
FFFF, FFFM, MFFF, FFMM, MFFM, MMFF, MMMM
Here F stands for a girl and M stands for a boy. The total number of queue satisfied the headmaster’s needs is 7. Can you make a program to find the total number of queue with n children?

 

 

Input

There are multiple cases in this problem and ended by the EOF. In each case, there is only one integer n means the number of children (1<=n<=1000)

 

 

Output

For each test case, there is only one integer means the number of queue satisfied the headmaster’s needs.

 

 

Sample Input

1
2
3

Sample Output

2
4
6

题意分析:

让n个人排队,女生不能单独站,也就是说女生的旁边一定有其他的女生,问一共有多少种排法。

解题思路:

1.当最后一个人是男生时,前面一个人没有要求,为a[n-1];

2.当最后一个人为女生时,前面的人必须为女生,为a[n-2];

但是有一种情况没有考虑: 男女|女女,满足第二种情况,但是因为a[n-2]中最后两个为 男女 不符合情况所以没有计算,

加上这种情况是a[n-4]。

所以可以推出a[n]=a[-1]+a[n-2]+a[n-4]。

#include <stdio.h>
#include <algorithm>
using namespace std;
#define N 1020
int ans[N][N], w[N];
void add(int a, int b)
{
	int temp=0, i;
	for(i=0; i<=max(w[a], w[b]); i++)
	{
		ans[a][i]+=ans[b][i]+temp;
		temp=ans[a][i]/10;
		ans[a][i]=ans[a][i]%10;
	}
	if(temp)
		ans[a][i++]=temp;
	w[a]=i-1;
}
int main()
{
	int i, n;
	ans[1][1]=1;
	ans[2][1]=2;
	ans[3][1]=4;
	ans[4][1]=7;
	w[1]=w[2]=w[3]=w[4]=1;
	for(i=5;i<=1000;i++)
	{
		add(i, i-1);
		add(i, i-2);
		add(i, i-4);
	}
	while(scanf("%d", &n)!=EOF)
	{
		for(i=w[n]; i>=1; i--)
			printf("%d", ans[n][i]);
		printf("\n");
	}
	return 0;
}

 

posted @ 2019-05-24 19:31  宿星  阅读(237)  评论(0编辑  收藏  举报