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 1, 3, 12, 60, 360... trong C++

Cho dãy: 1, 3, 12, 60, 360, 2520... Tìm số hạng thứ N.

Phân tích

Dãy tăng rất nhanh, liên quan đến giai thừa. Công thức: T_N = (N+1)! / 2.

Mã nguồn C++

#include <iostream>
using namespace std;

int calcFact(int n) {
    if (n == 1) return 1;
    return n * calcFact(n-1);
}

int findNTerm(int N) {
    return calcFact(N+1) / 2;
}

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

Đầu ra

8th term of the series is 181440