Processing math: 0%

已知等比数列的第 M 项和第 N 项,求第 P 项 (C++ 实现)


本题给定五个值 m、n、第 m 项、第 n 项和 p。我们的任务是:已知等比数列的第 M 项和第 N 项,求第 P 项。

对于一个等比数列,我们已知第 m 项和第 n 项的值。利用这些值,我们需要找到数列的第 p 项。

让我们来看一个例子来理解这个问题:

输入

m = 7, mthTerm = 1458, n = 10, nthterm = 39366, p = 3

输出

18

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

解决方案

这里,我们给定一个等比数列。假设等比数列为:

GP = a , a*r , a*(r2), a*(r3) ….

第 T 项的公式为

Tth Term = a * r(T-1)

现在,我们已知第 n 项和第 m 项:

mth term = a * (r ^ (m-1))
nth term = a * (r ^ (n-1))

将两个等式相除,我们得到:

mth term / nth term = (r ^(m - n))

使用这个等式,我们可以找到 r 的值,然后可以使用第 m 项的值来找到 a:

mth term = a * (r^(m-1))

然后在找到 a 和 r 的值后,可以使用以下公式轻松找到第 p 项的值:

pth term = a * (r^(p-1))

程序演示了我们解决方案的工作原理:

示例

 在线演示

#include <cmath>
#include <iostream>
using namespace std;
double findRinGP(double m, double n, double mth, double nth) {
   if (m < n)
      return pow(nth / mth, 1.0 / (n - m));
   return pow(mth / nth, 1.0 / (m - n));
}
double findTermGP(int m, int n, double mth, double nth, int p) {
   double r = findRinGP(m, n, mth, nth);
   double a = mth / pow(r, (m - 1));
   return ( a * pow(r, (p - 1)) );
}
int main() {
   int m = 7, n = 10, p = 5;
   double mth = 1458, nth = 39366;
   cout<<"The "<<p<<"th of the series is "<<findTermGP(m, n, mth, nth, p);
   return 0;
}

输出

The 5th of the series is 162

更新于:2021年3月16日

134 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告