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