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

Tìm số hạng thứ N của dãy 0, 7, 8, 33, 51... trong C++

Cho dãy: 0, 7, 8, 33, 51, 75, 102, 133... Tìm số hạng thứ N.

Công thức

T_N = 2*N^2 + N - 3.

Mã nguồn C++

#include <iostream>
#include <cmath>
using namespace std;

int nthTerm(int n) {
    return 2 * n * n + n - 3;
}

int main() {
    int N = 4;
    cout << nthTerm(N) << endl; // 33
    return 0;
}

Đầu ra

33