在 C++ 中找出數列 9、45、243、1377… 的第 N 項
在此問題中,我們給定一個整數值 N。我們的任務是找出數列的第 n 項 −
9、45、243、1377、8019、…
我們舉一個例子來理解這個問題,
Input : N = 4 Output : 1377
求解方案
根據觀察技術找出第 N 項是該問題的簡單解法。透過觀察該數列,我們可以 формулировать(俄語拼寫)如下 −
(11 + 21)*31 + (12 + 22)*32 + (13 + 23)*33 … + (1n + 2n)*3n
例項
演示我們解決方案的程式
#include <iostream> #include <math.h> using namespace std; long findNthTermSeries(int n){ return ( ( (pow(1, n) + pow(2, n)) )*pow(3, n) ); } int main(){ int n = 4; cout<<n<<"th term of the series is "<<findNthTermSeries(n); return 0; }
輸出
4th term of the series is 1377
廣告