Giới thiệu
Cho số nguyên N. Tìm số hạng thứ N của dãy: 7, 15, 32…
Ví dụ minh họa
Đầu vào
N = 6
Đầu ra
281
Giải thích
Dãy: 7, 15, 32, 67, 138, 281 T(2)=2×7+1, T(3)=2×15+2, T(n)=2×T(n-1)+(n-1)
Phương pháp giải
Công thức truy hồi: T(n) = 2×T(n-1) + (n-1), với T(1)=7. Tính lặp từ 2 đến N.
Chương trình minh họa
#include <iostream>
using namespace std;
int findNthTerm(int n) {
if (n == 1) return 7;
int termN = 7;
for (int i = 2; i <= n; i++)
termN = 2*termN + (i - 1);
return termN;
}
int main() {
int n = 12;
cout << "The series is 7, 15, 32, 67...\n";
cout << n << "th term of the series is " << findNthTerm(n);
return 0;
}
Đầu ra
The series is 7, 15, 32, 67... 12th term of the series is 18419