Mô tả bài toán
Cho N, tìm số hạng thứ N của dãy: 14, 28, 20, 40, 32, 64, 56, 112, ...
Ví dụ
N=6 → Output: 64.
Phân tích dãy
Dãy xen kẽ 2 dãy con:
- Vị trí lẻ: 14, 20, 32, 56,... công thức: T2k-1 = 14 + 6×(20+21+...+2k-2)
- Vị trí chẵn: 28, 40, 64, 112,... công thức: T2k = 28 + 6×(21+22+...+2k-1)
Cách 2: Quy tắc đệ quy đơn giản: N chẵn → TN = 2×TN-1; N lẻ → TN = TN-1 - 8.
Mã nguồn C++ (Cách 2)
#include <iostream>
using namespace std;
bool isEven(int N) { return N%2==0; }
int findNthTermSeries(int n) {
int TermN = 14;
for (int i=2; i<=n; i++) {
if (isEven(i)) TermN *= 2;
else TermN -= 8;
}
return TermN;
}
int main() {
int n = 15;
cout << n << "th term of the series is " << findNthTermSeries(n);
return 0;
}
Đầu ra
15th term of the series is 776