fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int n = 4;
  5. int a, b = 3, c = 2; // b = a_2, c = a_1
  6.  
  7. for (int i = 3; i <= n; i++) {
  8. a = 2 * b + c; // a_i = 2 * a_{i-1} + a_{i-2}
  9. c = b; // 次のステップのため、古い a_{i-1} を a_{i-2} の位置へ退避
  10. b = a; // 次のステップのため、新しい a_i を a_{i-1} の位置へ更新
  11. }
  12.  
  13. printf("数列a%dの値は%d\n", n, a);
  14. return 0;
  15. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
数列a4の値は19