fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int indent = 0;
  6.  
  7. string tabs()
  8. {
  9. return string(indent, '\t');
  10. }
  11.  
  12. int f(int x, int y)
  13. {
  14. cout << "f(" << x << "," << y << ") = ";
  15.  
  16. if (x <= 0) {
  17. cout << y << endl;
  18. return y;
  19. }
  20.  
  21. cout << "f(" << x - 1 << "," << y + 1 << ") - f(" << x / 2 << "," << y * 2 << ")" << endl;
  22. ++indent;
  23. cout << tabs() << "(" << endl << tabs();
  24.  
  25. int answer1 = f(x - 1, y + 1);
  26.  
  27. cout << tabs() << ")" << endl << tabs() << "(" << endl << tabs();
  28.  
  29. int answer2 = f(x / 2, y * 2);
  30.  
  31. cout << tabs() << ")" << endl;
  32. --indent;
  33.  
  34. int answer = answer1 - answer2;
  35.  
  36. cout << tabs() << "= (" << answer1 << ") - (" << answer2 << ") = " << answer << endl;
  37. return answer;
  38. }
  39.  
  40. int main()
  41. {
  42. int answer = f(4, -1);
  43. cout << endl << "final answer: " << answer;
  44. return 0;
  45. }
Success #stdin #stdout 0s 4528KB
stdin
Standard input is empty
stdout
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