fork download
  1. #include <stdio.h>
  2.  
  3. int process(int n, int *count) {
  4. static int cache[100] = {0};
  5. (*count)++;
  6.  
  7. if (n <= 1) return n;
  8. if (cache[n] != 0) return cache[n];
  9.  
  10. int result;
  11. if (n % 2 == 0) {
  12. result = process(n / 2, count) + process(n / 2 - 1, count);
  13. } else {
  14. result = process(n - 1, count) - process(n - 2, count);
  15. }
  16.  
  17. cache[n] = result;
  18. return result;
  19. }
  20.  
  21. int main() {
  22. int count = 0;
  23. int result = process(12, &count);
  24. printf("%d %d\n", result, count);
  25.  
  26. count = 0;
  27. result = process(12, &count);
  28. printf("%d %d\n", result, count);
  29. return 0;
  30. }
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
3 15
3 1