0

Could someone explain this?

#include <iostream> using namespace std; int t(int k) { if (k == 1) return 2; else if (k == 2) return 2; else return t(k - 1) + t(k - 2); } void main() { cout<<t(5); //returns 10 }

11th May 2018, 9:30 PM
Winter Soldier
Winter Soldier - avatar
1 Odpowiedź
0
try to draw a tree of invokes. at the top, there is 5. then, there are 2 branches, 4 and 3. feom 3 goes 2 and 1, from 4 goes 3 and 2. from 3 goes 2 and 1. then see what returns what and add thing together. See what recursion is
11th May 2018, 9:36 PM
Paul