Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

หาพจน์ที่ N ของอนุกรม 0, 7, 8, 33, 51, 75, 102, 133... ใน C++

หาพจน์ที่ N ของอนุกรม: 0, 7, 8, 33, 51, 75, 102, 133, ...

ตัวอย่าง: N = 4 → 33

วิธีแก้ปัญหา (Quadratic Formula)

สูตรพจน์ที่ n:

Tn = 2×n² + n - 3

ตัวอย่างโค้ด C++

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

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

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

Output

33