fork download
  1. #include <stdio.h>
  2. //練習問題G
  3.  
  4. int func(int n){
  5. if(n==1){
  6. return 1;
  7. }
  8. else if(n==2){
  9. return 2;
  10. }
  11. else{
  12. int a, b=2, c=1;
  13. int i;
  14. for(i=2;i<n;i++){
  15. a=-2*b+2*c;
  16. c=b;
  17. b=a;
  18.  
  19. }
  20. return a;
  21. }
  22. }
  23. int main(void) {
  24. int n = 3;
  25. printf("数列anについて, n=%dのときの値は%d\n", n, func(n));
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
数列anについて, n=3のときの値は-2