1 2 3 4 5
|
int f(int x, int y) {
if(x < 0 || y < 0)
return x - y;
else
return f(x - 1, y) + f(x, y -1);
|
if i want to calculate the output of f(2,1) by hand, how should i do it?
i started with this: f(2,1) -> return f(1,1) + f(2,0)
then f(1,1) -> return f(0,1) + f(1,0)
then f(0,1) -> return f(-1,1) + f(0,0) (from f(-1,1) return -2)
then f(0,0) -> return f(-1,0) + f(0,-1) (from f(-1,0) return -1; from (0,-1) return 1)
i do the same with f(2,1), f(1,0), etc. but i come up with an answer different from my compiler's answer (it gives 5).
what should be the correct steps to find the value?
thanks!