C++ 程式找出 5、13、25、41、61… 等數列的第 N 項


本題中,給了我們一個整數 N。我們的任務是編寫一個程式,找數列 5、13、25、41、61 中的第 N 項...

我們舉個例子,以理解題意:

輸入

N = 5

輸出

61

說明

數列為 − 5、13、25、41、61...

解題思路

解決該問題的簡單方法是使用數列第 n 項的一般公式。第 N 項為:

Nth term = ( (N*N) + ((N+1)*(N+1)) )

一個程式來說明我們的解題思路:

示例

 執行演示

#include <iostream>
using namespace std;
int calcNthTerm(int N) {
   return ( ( (N + 1)*( N + 1) ) + (N*N) ) ;
}
int main() {
   int N = 7;
   cout<<N<<"th term of the series is "<<calcNthTerm(N);
   return 0;
}

輸出

7th term of the series is 113

更新日期: 2021-3-15

178 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告