加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

HDU 1297 Children’s Queue(含整型大数模板)

发布时间:2020-12-14 02:32:42 所属栏目:大数据 来源:网络整理
导读:Children’s Queue Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12019????Accepted Submission(s): 3912 Problem Description There are many students in PHT School. One day,the headmas

Children’s Queue

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12019????Accepted Submission(s): 3912


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
  
  
1 2 4
?

以下的都是从不同博客取最好的转载来的= =


1.解释:

按照最后一个人的性别分析,他要么是男,要么是女,所以可以分两大类讨论:

1、如果n个人的合法队列的最后一个人是男,则对前面n-1个人的队列没有任何限制,他只要站在最后即可,所以,这种情况一共有F(n-1);

2、如果n个人的合法队列的最后一个人是女,则要求队列的第n-1个人务必也是女生,这就是说,限定了最后两个人必须都是女生,这又可以分两种情况;

2.1、如果队列的前n-2个人是合法的队列,则显然后面再加两个女生,也一定是合法的,这种情况有F(n-2);

2.2、但是,难点在于,即使前面n-2个人不是合法的队列,加上两个女生也有可能是合法的,当然,这种长度为n-2的不合法队列,不合法的地方必须是尾巴,就是说,这里说的长度是n-2的不合法串的形式必须是“F(n-4)++,这种情况一共有F(n-4).

所以,通过以上的分析,可以得到递推的通项公式: F(n)=F(n-1)+F(n-2)+F(n-4) ? (n>3)然后就是对n<=3?的一些特殊情况的处理了,显然:F(0)=1?? (没有人也是合法的,这个可以特殊处理,就像0的阶乘定义为1一样) ? F(1)=1? ? F(2)=2?? ? F(3)=4

?

F(n)=F(n-1)+F(n-2)+F(n-4)


2.代码

#include<iostream>
#include<string> 
using namespace std;
//大数相加,可以用以下模版解决。熟记模版很重要~~
string add(string s1,string s2)
{
    
    int j,l,la,lb;
    string max,min;
    max=s1;min=s2;
    if(s1.length()<s2.length()) {max=s2;min=s1;}
    la=max.size();lb=min.size();
    l=la-1;
    for(j=lb-1;j>=0;j--,l--) max[l] += min[j]-'0'; 
    for(j=la-1;j>=1;j--) if(max[j]>'9'){max[j]-=10;max[j-1]++;}
    if(max[0]>'9') {max[0]-=10;max='1'+max;}
    return max;
}
int main(){
	int n,i;
	string a[1001];
	a[0]="1";
	a[1]="1";
	a[2]="2";
	a[3]="4";
	for(i=4;i<1001;++i)
	   a[i]=add(add(a[i-1],a[i-2]),a[i-4]);
	 while(cin>>n)
	    cout<<a[n]<<endl;
	return 0;	  
}



下面是java代码:

import java.math.*;
import java.math.BigDecimal;
import java.io.*;
import java.util.*;
class Main 
{
    public static void main(String[] args)
    {
       Scanner cin=new Scanner(System.in);
       BigInteger f[]=new BigInteger[1010];
       f[0]=BigInteger.valueOf(1);
       f[1]=BigInteger.valueOf(1);
       f[2]=BigInteger.valueOf(2);
       f[3]=BigInteger.valueOf(4);
       for(int i=4;i<=1000;i++)
        f[i]=f[i-1].add(f[i-2].add(f[i-4]));
       int n;
       while(cin.hasNext())
       {
        n=cin.nextInt();
        System.out.println(f[n]);
       }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读