Children’s Queue

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)题目大意
排队。F代表女孩,M代表男孩。男女生排队,这个队列中可以没有女孩,如果有女孩,不能自己站着,即得有陪同。
2)解题分析:
递推+大数
计算F(n)
1.当最后一个是男孩时,前n-1个随便排,只要符合要求,即F(n-1);
2.当最后一个时女孩时,第n-1个肯定是女孩,分两种情况:
a.前n-2个同前n-1那样,即F(n-2);
b.若前面n-2个人不是合法的队列,加上后两个女生也有可能是合法的。当第n-2是女孩而n-3是男孩的情况,可能合法,情况总数为F(n-4);
(参考http://blog.csdn.net/xujinsmile/article/details/7364307

也可找规律
1->1
2->2
3->4
4->7
5->12
即F(n)=F(n-1)+F(n-2)+F(n-4).

代码

import java.util.*;
import java.math.*;

public class Main {

    public static void main(String[] args) {
        BigInteger f[]=new BigInteger[1001];
        //同斐波那契数列,先大数打表
        f[0]=new BigInteger("1");
        f[1]=new BigInteger("1");
        f[2]=new BigInteger("2");
        f[3]=new BigInteger("4");
        for(int i=4;i<=1000;i++)
        {
            f[i]=f[i-1].add(f[i-2]).add(f[i-4]);
        }
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){//输入,同C,EOF结束
            int m=sc.nextInt();
            System.out.println(f[m]);
        }   
    }
}

Children's Dining

10-26

DescriptionnnUsually children in kindergarten like to quarrel with each other. This situation annoys the child-care women. For instant, when diner time comes, a fierce conflict may break out when a certain couple of children sitting side by side who are hostile with each other. Although there aren't too many children dining at the same round table, but the relationship of "enemy" or "friend" may be very complex. The child-care women do come across a big problem. Now it is time for you to help them to figure out a proper arrangement of sitting, with which no two "enemy" children is adjacent. nnNow we assume that there are 2 * n children who sit around a big table, and that none has more than n - 1 "enemies".nInputnnThe input is consisted of several test blocks. For each block, the first line contains two integers n and m (1 <= n <= 200, 0 <= m <= n (n - 1)). We use positive integers from 1 to 2 * n to label the children dining round table. Then m lines followed. Each contains positive integers i and j ( i is not equal to j, 1 <= i, j <= 2 * n), which indicate that child i and child j consider each other as "enemy". In a input block, a same relationship isn't given more than once, which means that if "i j" has been given, "j i" will not be given. nnThere will be a blank line between input blocks. And m = n = 0 indicates the end of input and this case shouldn't be processed.nOutputnnFor each test block, if the proper arrangement exist, you should print a line with a proper one; otherwise, print a line with "No solution!".nSample Inputnn1 0nn2 2n1 2n3 4nn3 6n1 2n1 3n2 4n3 5n4 6n5 6nn4 12n1 2n1 3n1 4n2 5n2 6n3 7n3 8n4 8n4 7n5 6n5 7n6 8nn0 0nSample Outputnn1 2n4 2 3 1n1 6 3 2 5 4n1 6 7 2 3 4 5 8

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭