fork download
  1. #include <stdio.h>
  2.  
  3. #define MaxN 100
  4.  
  5. int m[3][MaxN];
  6.  
  7. int f(int n)
  8. {
  9. int i;
  10.  
  11. m[0][1] = m[1][1] = m[2][1] = 1;
  12.  
  13. for (i = 2; i <= n; i++)
  14. {
  15. m[0][i] = m[0][i-1] + m[1][i-1] + m[2][i-1];
  16. m[1][i] = m[0][i-1] + m[2][i-1];
  17. m[2][i] = m[0][i-1] + m[1][i-1];
  18. }
  19.  
  20. return m[0][n] + m[1][n] + m[2][n];
  21. }
  22.  
  23. int main(int argc, char **argv)
  24. {
  25. printf("f(10) = %d\n", f(10));
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0s 1832KB
stdin
Standard input is empty
stdout
f(10) = 8119