Test instructions: There is a bee, every year the drone produces a females and drone, and then die, females produce a drone, and then die, there is not a dead females, ask the number of drones and the total number of bees after n years
Idea: recursion + test Instructions
The number of drones this year is m[x], last year was m[x-1],
The number of females this year is f[x], last year for F[x-1]
According to test instructions, you can get:
M[X]=F[X-1]+M[X-1];
F[x]=m[x-1]+1
PS: According to recursion can also get the number of females to meet the Fibonacci sequence, namely: F[x]=f[x-1]+f[x-2]
Code:
1#include <iostream>2#include <cstdio>3 using namespacestd;4 Long LongN;5 #defineMAXN 1006 Long LongF[MAXN],M[MAXN];7 8 BOOLdatecin ()9 {Ten if(SCANF ("%lld", &n)! =EOF) One { A if(n==-1) - return false; - return true; the } - return false; - } - + voiddatecal () - { + inti; Af[0]=1, m[0]=0, f[1]=1, m[1]=1; at for(i=2; i<maxn;i++) - { -f[i]=f[i-1]+f[i-2]; -m[i]=m[i-1]+f[i-1]; - } - } in - voidshowres () to { +printf"%lld%lld\n", m[n],m[n]+f[n]); - } the intMain () * { $ datecal ();Panax Notoginseng while(Datecin ()) - { the showres (); + } A return 0; the}
UVA, 11000 Bee