Giới thiệu
Cho số nguyên N. Tìm số hạng thứ N của dãy: 1, 4, 27, 16, 125, 36, 343…
Ví dụ minh họa
Đầu vào
N = 7
Đầu ra
343
Giải thích
Dãy: 1, 4, 27, 16, 125, 36, 343... Chỉ số lẻ: 1³, 3³, 5³, 7³... Chỉ số chẵn: 2², 4², 6²...
Phân tích dãy
Công thức chung:
- N lẻ: T(N) = N³
- N chẵn: T(N) = N²
Chương trình minh họa
#include <iostream>
using namespace std;
int findNthTerm(int N) {
if (N % 2 == 0)
return N * N;
return N * N * N;
}
int main() {
int N = 8;
cout << "The " << N << "th term of the series is " << findNthTerm(N);
return 0;
}
Đầu ra
The 8th term of the series is 64