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. if(n == 1){
  8. return c;
  9. }
  10. if(n == 2){
  11. return b;
  12. }
  13. for(i = 3; i <= n; i++){
  14. a = -2 * b + 2 * c;
  15. c = b;
  16. b = a;
  17. }
  18. return a;
  19. }
  20.  
  21.  
  22. int main(void) {
  23. int n = 3;
  24. printf("数列anについて, n=%dのときの値は%d\n", n, func(n));
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
数列anについて, n=3のときの値は-2