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