程序在C ++中找到系列0、0、2、1、4、2、6、3、8…的第N个项
在本教程中,我们将讨论一个程序以查找系列0、0、2、1、1、4、2、6、3、8的第N个项。
为此,我们将提供一个号码。我们的任务是在特定位置找到给定系列的术语。
示例
#include <iostream>#include <math.h>
using namespace std;
//查找给定系列的第n个项
void findNthTerm(int n) {
//如果甚至
if (n % 2 == 0) {
n = n / 2;
n = 2 * (n - 1);
cout << n / 2 << endl;
}
//如果是奇数
else {
n = (n / 2) + 1;
n = 2 * (n - 1);
cout << n << endl;
}
}
int main() {
int X = 10;
findNthTerm(X);
X = 7;
findNthTerm(X);
return 0;
}
输出结果
46
以上是 程序在C ++中找到系列0、0、2、1、4、2、6、3、8…的第N个项 的全部内容, 来源链接: utcz.com/z/321721.html