C++ 程式,用於找出 1、6、15、28、45、… 系列中的第 N 個數


在此問題中,我們將獲得一個整數值 N。我們的任務是建立一個程式,以找出 1、6、15、28、45、… 系列中的第 N 個數。

在這個系列中,每個元素都比前一個和後一個元素的平均值小 2。

讓我們舉個例子來理解這個問題,

輸入

N = 5

輸出

45

解決方案方法

1、6、15、28、45、… 系列中的第 N 項可以使用以下公式找到:

TN = 2*N*N - N

一個程式說明了我們解決方案的工作原理,

示例

 現場演示

#include <iostream>
using namespace std;
#define mod 1000000009
int calcNthTerm(long n) {
   return (((2 * n * n) % mod) - n + mod) % mod;
}
int main(){
   long N = 19;
   cout<<N<<"th Term of the series is "<<calcNthTerm(N);
   return 0;
}

輸出

19th Term of the series is 703

更新於: 2021 年 3 月 13 日

249 次瀏覽

助力你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.