NYOJ 923 Children’s Queue

来源:互联网 发布:adobeair是什么软件 编辑:程序博客网 时间:2024/05/22 08:47

Children’s Queue

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述
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?
输入
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)
输出
For each test case, there is only one integer means the number of queue satisfied the headmaster’s needs.
样例输入
123
样例输出
12

4


**借鉴来源  http://blog.csdn.net/xujinsmile/article/details/7364307

计算F(n):

一:当最后一个是男孩M时候,前面n-1个随便排出来,只要符合规则就可以,即是F(n-1);

二:当最后一个是女孩F时候,第n-1个肯定是女孩F,这时候又有两种情况:

        1)前面n-2个可以按n-2个的时候的规则来,完全可以,即是F(n-2);

        2)但是即使前面n-2个人不是合法的队列,加上两个女生也有可能是合法的。当第n-2是女孩而n-3是男孩的情况,可能合法,情况总数为F(n-4);

综上所述:总数F(n)=F(n-1)+F(n-2)+F(n-4);并且,F(0)=1,F(1)=1,F(2)=2,F(3)=4。


#include <iostream>#include <string>using namespace std;string add(string s1,string s2)//大数加法 {int i,l,la,lb;string max,min;if(s1.size()>=s2.size()){max = s1;min = s2;}else{max = s2;min = s1;}la = max.size();lb = min.size();l = la - 1;for(i=lb-1;i>=0;i--,l--){max[l] += min[i]-'0';}for(i=la-1;i>=1;i--){if(max[i]>'9'){max[i] -=10;max[i-1] += 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";a[4] = "7";for(i=5;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;}


奈何我冒泡的算法如果打动你超时的心!!

0 0
原创粉丝点击