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{
  14. for(i=0;i<n;i++){
  15. a=-2*b+2*c;
  16. c=b;
  17. b=a;
  18. }
  19. return a;
  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 0s 5280KB
stdin
Standard input is empty
stdout
数列anについて, n=3のときの値は-20