程序在C ++中找到系列1 4 15 24 45 60 92 ...的N个项
在本教程中,我们将讨论一个程序来查找系列141524456092的N个项。
为此,我们将提供一个号码。我们的任务是在特定位置找到给定系列的术语。
示例
#include <stdio.h>
//计算给定序列的第n个项
long long int nthTerm(long long int n) {
long long int nth;
//如果n是偶数
if (n % 2 == 0)
nth = 2 * ((n * n) - n);
//如果n是奇数
else
nth = (2 * n * n) - n;
return nth;
}
int main() {
long long int n;
n = 5;
printf("%lld\n", nthTerm(n));
n = 25;
printf("%lld\n", nthTerm(n));
n = 25000000;
printf("%lld\n", nthTerm(n));
n = 250000007;
printf("%lld\n", nthTerm(n));
return 0;
}输出结果
45 1225 1249999950000000 125000006750000091
热门推荐
10 收到公公生日祝福语简短
11 死人新年祝福语简短句
12 求婚文案 祝福语大全简短
13 生日祝福语简短蛋糕上
14 侄女婚礼语音祝福语简短
15 学生祝福语简短古诗句
16 毕业结婚祝福语简短精辟
17 出院家庭花束祝福语简短
18 福州疫情祝福语简短英语