Skip to content

Instantly share code, notes, and snippets.

@mryoshio
Created November 12, 2014 11:15
Show Gist options
  • Save mryoshio/30d3bc9d748330bcadb3 to your computer and use it in GitHub Desktop.
Save mryoshio/30d3bc9d748330bcadb3 to your computer and use it in GitHub Desktop.
Fibonacci number with DP
#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