C++ 程序查找级数 3, 14, 39, 84… 的第 N 项
在本问题中,我们给出一个整数 N,我们的任务是创建一个程序来查找级数 3, 14, 39, 84… 的第 N 项
我们举个例子来理解这个问题
输入
N = 4
输出
84
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
说明
4次项 − ((4 * 4 * 4) + (4 * 4) + 4) = 64 + 16 + 4 = 84
解决方案方法
解决这个问题的一个简单方法是使用该级数第 n 项的一般公式。公式为:
Nth term = ( (N*N*N) + (N*N) + (N))
程序说明我们解决方案的具体工作原理
示例
#include <iostream> using namespace std; int calcNthTerm(int N) { return ( (N*N*N) + (N*N) + (N) ); } int main() { int N = 6; cout<<N<<"th term of the series is "<<calcNthTerm(N); return 0; }
输出
6th term of the series is 258
广告