fork download
  1. #include <stdio.h>
  2.  
  3. int c = 0;
  4.  
  5. int rec(int n) {
  6.  
  7. c++;
  8.  
  9. // ベースケース
  10. if (n == 1) {
  11. return 1;
  12. } else if (n == 2) {
  13. return 2;
  14. } else {
  15.  
  16. return -6 * rec(n - 1) - 9 * rec(n - 2);
  17. }
  18. }
  19.  
  20. int main(void) {
  21. int n = 5;
  22.  
  23. int value = rec(n);
  24. printf("数列a%dの値は%d\n", n, value);
  25.  
  26. printf("このときrecの呼び出し回数は%d\n", c);
  27.  
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
数列a5の値は-459
このときrecの呼び出し回数は9