hdu 1297 Children’s Queue
2014-08-03 20:31
267 查看
Children’s Queue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10550 Accepted Submission(s): 3392
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
计算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 a,string b) //大数加法 { string Max; string Min; if(a.size()>b.size()) { Max=a; Min=b; } else { Max=b; Min=a; } long j=Max.size()-1; for (long i=Min.size()-1;i>=0; i--,j--) { Max[j]+=Min[i]-'0'; } for (j=Max.size()-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; string a[1001]; a[0]='1'; a[1]='1'; a[2]='2'; a[3]='4'; for (int i=4; i<1001; i++) { a[i]=add(add(a[i-1],a[i-2]),a[i-4]); } while (cin>>n) { cout<<a <<endl; }<span style="white-space:pre"> </span> return 0; }
相关文章推荐
- hdu 1297 Children’s Queue(找规律+高精度)
- 【HDU】1297 - Children’s Queue(BigDecimal & 递推 & 思维)
- HDU 1297 Children’s Queue
- hdu 1297 Children’s Queue(递推和高精)
- HDU-1297 Children’s Queue(递推)(高精度)
- HDU 1297 Children’s Queue
- HDU 1297 Children’s Queue JAVA *
- HDOJ/HDU 1297 Children’s Queue(推导~大数)
- hdu 1297 Children’s Queue(大数处理)
- HDUACM 1297 Children’s Queue(递…
- HDOJ/HDU 1297 Children’s Queue(推导~大数)
- HDU 1297 Children’s Queue
- HDU 1297 Children’s Queue
- hdu 1297 Children’s Queue
- HDU 1297 Children’s Queue
- hdu 1297 Children’s Queue
- HDU 1297 Children’s Queue
- hdu 1297 Children’s Queue(java+简单dp)
- HDU—— 1297 Children’s Queue
- HDU 1297 Children’s Queue