hdu-Fibonacci

Problem Description
In 2007 he arrived. After 2006, a year of practice, mathematical prodigy zouyu finally Fibonacci sequence 0-100000000 of
(f [0] = 0, f [1] = 1; f [i] = f [i-1] + f [i- 2] (i> = 2) ) for all values back down.
Next, CodeStar decided to quiz him, and asked him a number for each, he said it would take the answer, but some digital too long. So long as the provisions of more than four say the front four on it, but CodeStar himself can not remember. So he decided to write a program to test zouyu say is correct.
 
Input
A plurality of input numbers n (0 <= n <= 100000000), each digit line. Read the file.
 
Output
Output f [n] of the first four digits (if less than 4 digits, on all output).
 
Sample Input
0 1 2 3 4 5 35 36 37 38 39 40
 
Sample Output
0 1 1 2 3 5 9227 1493 2415 3908 6324 1023
  log10 (an) = - 0.5 * log10 (5.0) + ((double) n) * log (f) / log (10.0) + log10 (1 - ((1-√5) / (1 + √5)) ^ n) = where F (sqrt (5.0) +1.0) /2.0;
loglO (. 1 - ((. 1-√5) / (+ √5. 1)) ^ n) -> 0
can be written as log10 (an) = - 0.5 * log10 (5.0) + ( (double) n) * log (f) / log (10.0);
finally whichever fractional part.
----------------

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector> 
#include <cmath>
using namespace std;
int main()  
{  
    int f[22]={0,1,1};  
    for (int i = 3 ; i <= 20 ; i++)  
        f[i] = f[i-1] + f[i-2];  
    double n;  
    double a1 = log10(1.0 / sqrt(5));  
    double a2 = log10((1 + sqrt(5)) / 2);  
    while (cin>>n)  
    {  
        if (n <= 20)  
        {  
            cout<<f[(int)n]<<endl;  
            continue;  
        Years - = floor (years);  
        }  
        Dual years = a1 * a2 + n;  
        years = pow (10 years);  
        Yr = (int) (* 1000 years);  
        cout << endl << years;  
    }   
    Return 0;  
}

 

 

Guess you like

Origin www.cnblogs.com/yelir/p/11588693.html