在 C++ 中查找系列 0, 8, 64, 216, 512,... 的第 n 项
在这个问题中,我们给定一个整数 N。我们的任务是找到该系列的第 n 项 -
0, 8, 64, 216, 512, 1000, 1728, 2744…
让我们举个例子来理解这个问题,
Input: N = 6 Output: 1000
解决方案方法
为了找到该系列的第 N 项,我们需要仔细观察该系列。该系列是偶数的立方,其中第一项为 0。
因此,该系列可以解码为 -
[0]3, [2]3, [4]3, [6]3, [8]3, [10]3…
对于第 i 项,
T1 = [0]3 = [2*(1-1)]3
T2 = [2]3 = [2*(2-1)]3
T3 = [4]3 = [2*(3-1)]3
T4 = [6]3 = [2*(4-1)]3
T5 = [8]3 = [2*(5-1)]3
因此,该系列的第 N 项是 { [2*(N-1)]3 }
示例
程序说明我们解决方案的工作原理
#include <iostream>
using namespace std;
long findNthTermSeries(int n){
return ((2*(n-1))*(2*(n-1))*(2*(n-1)));
}
int main(){
int n = 12;
cout<<n<<"th term of the series is "<<findNthTermSeries(n);
return 0;
}输出
12th term of the series is 10648
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP