Children’s Queue
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 80 Accepted Submission(s) : 19
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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?
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
#include<iostream>
#include<string>
using namespace std;
string f(string s1,string s2)
{
int j,l,p,q,m,n;
string max,min;//将两个大数用字符串表示
max=s1;
min=s2;
m=s1.length();//分别取其长度
n=s2.length();
if(m<n)
{
max=s2;
min=s1;
}
p=max.size();
q=min.size();
l=p-1;
for(j=q-1;j>=0;j--,l--)//从最低位开始,每一位对应相加,并且max保存和
max[l] += min[j]-'0';
for(j=p-1;j>=1;j--) //整数部分
if(max[j]>'9')
{
max[j]-=10;//如果两个数字相加大于9,则进一位
max[j-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";
for(i=4;i<1001;++i)
a[i]=f(f(a[i-1],a[i-2]),a[i-4]);
while(cin>>n)
cout<<a[n]<<endl;
return 0;
}