Question: Construct A 01 string so that 1 is not adjacent and ask how many strings with the length of N are.
Analysis: Mathematical and recursive series.
If n strings are set to N, there is a recursive relationship: F (n) = f (n-1) + f (n-2 );
The end of N may be 0 or 1:
If the end is 0, the front is 0 or 1, so it can be f (n-1 );
If the end is 1, the first must be 0, then more casual, so f (n-2 );
This is obviously the recurrence formula of FIB, F (n) = fib (n + 1 ).
Note: use long to prevent overflow.
#include <iostream>#include <cstdlib>using namespace std;long long Fib[100];int main(){Fib[1] = Fib[0] = 1LL;for (int i = 2 ; i < 55 ; ++ i)Fib[i] = Fib[i-1]+Fib[i-2];int n,m;while (cin >> n) for (int i = 1 ; i <= n ; ++ i) {cin >> m;cout << "Scenario #" << i << ":\n" << Fib[m+1] << "\n\n";}return 0;}
Ultraviolet A 10450-World Cup Noise