C++ 程式尋找級數 1, 8, 54, 384… 的第 N 項
在這個問題中,我們給定一個整數 N。我們的任務是建立一個程式來尋找數列 1,8, 54, 384 ... 的第 N 項
我們舉個例子來理解這個問題,
輸入
N = 4
輸出
384
解釋
第 4 項 − (4 * 4 * (4!) = 384
解決方案方法
解決此問題的簡單方法是使用數列第 n 項的一般公式。公式如下,
Nth term = ( N * N * (N !) )
程式說明我們解決方案的工作原理,
示例
#include <iostream>
using namespace std;
int calcFact(int N) {
int fact = 1;
for (int i = 1; i <= N; i++)
fact = 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
廣告
資料結構
資料網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
安卓
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP