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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP