#include <iostream> #include <string> using namespace std; int indent = 0; string tabs() { return string(indent, '\t'); } int f(int x, int y) { cout << "f(" << x << "," << y << ") = "; if (x <= 0) { cout << y << endl; return y; } cout << "f(" << x - 1 << "," << y + 1 << ") - f(" << x / 2 << "," << y * 2 << ")" << endl; ++indent; cout << tabs() << "(" << endl << tabs(); int answer1 = f(x - 1, y + 1); cout << tabs() << ")" << endl << tabs() << "(" << endl << tabs(); int answer2 = f(x / 2, y * 2); cout << tabs() << ")" << endl; --indent; int answer = answer1 - answer2; cout << tabs() << "= (" << answer1 << ") - (" << answer2 << ") = " << answer << endl; return answer; } int main() { int answer = f(4, -1); cout << endl << "final answer: " << answer; return 0; }
Standard input is empty
f(4,-1) = f(3,0) - f(2,-2) ( f(3,0) = f(2,1) - f(1,0) ( f(2,1) = f(1,2) - f(1,2) ( f(1,2) = f(0,3) - f(0,4) ( f(0,3) = 3 ) ( f(0,4) = 4 ) = (3) - (4) = -1 ) ( f(1,2) = f(0,3) - f(0,4) ( f(0,3) = 3 ) ( f(0,4) = 4 ) = (3) - (4) = -1 ) = (-1) - (-1) = 0 ) ( f(1,0) = f(0,1) - f(0,0) ( f(0,1) = 1 ) ( f(0,0) = 0 ) = (1) - (0) = 1 ) = (0) - (1) = -1 ) ( f(2,-2) = f(1,-1) - f(1,-4) ( f(1,-1) = f(0,0) - f(0,-2) ( f(0,0) = 0 ) ( f(0,-2) = -2 ) = (0) - (-2) = 2 ) ( f(1,-4) = f(0,-3) - f(0,-8) ( f(0,-3) = -3 ) ( f(0,-8) = -8 ) = (-3) - (-8) = 5 ) = (2) - (5) = -3 ) = (-1) - (-3) = 2 final answer: 2