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