fork download
  1. #include <stdio.h>
  2. //練習問題G
  3.  
  4. int func(int n){
  5. int a, b=2, c=1;
  6. int i;
  7.  
  8. if(n==1)
  9. {
  10. return c;
  11. }
  12. else if(n==2)
  13. {
  14. return b;
  15. }
  16.  
  17. for(i=0;i<n;i++)
  18. {
  19. a=-2*b+2*c;
  20. c=b;
  21. b=a;
  22. }
  23. return a;
  24. }
  25.  
  26. int main(void)
  27. {
  28. int n = 3;
  29. printf("数列anについて, n=%dのときの値は%d\n", n, func(n));
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
数列anについて, n=3のときの値は-20