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

72 篇文章 0 订阅
12 篇文章 1 订阅

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;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张宜强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值