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