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

หาพจน์ที่ N ของอนุกรม 1, 8, 54, 384... ด้วย C++


ภาพรวมปัญหา

หาพจน์ที่ N ของอนุกรม: 1, 8, 54, 384, ...

ตัวอย่าง

อินพุต

N = 4

เอาต์พุต

384

คำอธิบาย

4 × 4 × 4! = 16 × 24 = 384

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

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

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

#include <iostream>
using namespace std;

int calcFact(int N) {
    int fact = 1;
    for (int i = 1; i <= N; i++)
        fact *= i;
    return fact;
}

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

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

เอาต์พุต

5th term of the series is 3000

สรุป

เกี่ยวข้องกับ Factorial คำนวณ N! แล้วคูณ N²