Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Chương trình C++ tìm số hạng thứ N của dãy 9, 33, 73, 129…

Giới thiệu

Cho số nguyên N. Tìm số hạng thứ N của dãy: 9, 33, 73, 129…

Ví dụ minh họa

Đầu vào

N = 4

Đầu ra

129

Giải thích

Dãy đến số hạng 4: 9, 33, 73, 129...

Phân tích dãy

Phép trừ dãy dịch chuyển: t(n) = 8n² + 1.

Chương trình minh họa

#include <iostream>
using namespace std;

int findNthTerm(int n) {
    return 8*n*n + 1;
}

int main() {
    int n = 12;
    cout << "The series is 9, 33, 73, 129...\n";
    cout << n << "th term of the series is " << findNthTerm(n);
    return 0;
}

Đầu ra

The series is 9, 33, 73, 129...
12th term of the series is 1153