Created
November 12, 2014 11:15
-
-
Save mryoshio/30d3bc9d748330bcadb3 to your computer and use it in GitHub Desktop.
Fibonacci number with DP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
int main() { | |
long n; | |
cout << "n: "; cin >> n; | |
if (n == 0 || n == 1) { | |
cout << "fibo(" << n << ") is " << n << endl; | |
return 0; | |
} | |
vector<long> v(n, 1); | |
v[0] = 0; v[1] = 1; | |
for (long i = 2; i < n; i++) | |
v[i] = v[i-1] + v[i-2]; | |
cout << "fibo(" << n << ") is " << v[n-1] << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment