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