V - Children’s Queue HDU - 1297

 V - Children’s Queue

HDU - 1297

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

1
2
4

题意:问女生至少有两个站在一起的排队情况有多少种(可以没有女生)

思路:  递推好题。不断地从大问题递归成小问题。

对于n个人(n>=3),从后面往前面递推,排在最后有两种可能。

1、最后一个人是男生。

              1.1、那么在前面n-1个人排队符合要求的情况下直接加上最后这个男生都符合。那么问题就转化为求f(n-1),即n-1个人排队的问题。有f(n-1)种情况

               1.2、如果前面n-1个人排队不符合条件,那么加上最后一个男生依然不符合条件。

2、最后一个人是女生。那么倒数第二个必须是女生。

            2.1、那么前面n-2个人排队符合要求的情况下,加入倒数第二个女生+倒数一个女生,这种情况当然是符合要求的,即转化为求f(n-2)的问题。

            2.2、如果前面n-2个人排队不符和要求的情况下,加入两个两个女生否符合呢?这个是存在有可能的情况的,这种情况是当且仅当倒数第三个为女生,而倒数第4个为男生下,前n-4的排队符合条件,即f(n-4)+男生+女生,虽然这种n-1个人的排队是不不符合的,但加上后面两个女生是符合条件的(这点有点难想到)。即转化为求f(n-1)的问题。

#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=1044;
const int mmax=555;
const int MOD = 1e5+ 7;
int num[N][mmax];//数组保留对应位上的数字
int main()
{
  int n;
  memset(num,0,sizeof(num));
  num[1][0]=1;
  num[2][0]=2;
  num[3][0]=4;
  num[4][0]=7;
  int mod=0;
  for(int i=5;i<1011;i++){
    for(int j=0;j<534;j++){
        mod+=num[i-1][j]+num[i-2][j]+num[i-4][j];
        num[i][j]=mod%100000;
        mod/=100000;
    }
  }
  while(scanf("%d",&n)!=EOF){
    int k=533;
    while(!num[n][k])k--;
    printf("%d",num[n][k]);
    for(int i=k-1;i>=0;i--)
        printf("%05d",num[n][i]);
    printf("\n");
  }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/clz16251102113/article/details/81226725
v s
今日推荐