Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- using namespace std;
- map<int, int> fibo;
- int fibonacci( int n )
- {
- if ( n == 0 || n == 1 ){ return 1; }
- map<int,int>::iterator itr = fibo.find( n );
- if ( itr != fibo.end() )
- return itr->second;
- else
- return fibo[ n ] = fibonacci( n -1 ) + fibonacci( n - 2 );
- }
- int main()
- {
- int n;
- while(cin>>n)
- {
- cout<<fibonacci(n)<<endl;
- }return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement