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

หาพจน์ที่ N ของอนุกรม 3, 14, 39, 84... ด้วย C++


ภาพรวมปัญหา

หาพจน์ที่ N ของอนุกรม: 3, 14, 39, 84, ...

ตัวอย่าง

อินพุต

N = 4

เอาต์พุต

84

คำอธิบาย

4³ + 4² + 4 = 64 + 16 + 4 = 84

วิธีแก้ปัญหา

สูตรทั่วไป: T(N) = N³ + N² + N

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

#include <iostream>
using namespace std;

int calcNthTerm(int N) {
    return (N * N * N) + (N * N) + N;
}

int main() {
    int N = 6;
    cout << N << "th term of the series is " << calcNthTerm(N);
    return 0;
}

เอาต์พุต

6th term of the series is 258

สรุป

สูตรพหุนามดีกรี 3 คำนวณได้ทันที O(1)