东方博宜OJ 1145:数列求和
【题目来源】
https://oj.czos.cn/p/1145
【题目描述】
有一数列如下:1 2 4 7 11 16 22 ...,试求该数列前 N 项之和。
【输入格式】
一个整数 N(0<N<1000)。
【输出格式】
一个整数。
【输入样例】
6
【输出样例】
41
【数据范围】
0<N<1000。
【算法分析】
● 数列 1, 2, 4, 7, 11, 16,... 的递推规律:a₁=1、aᵢ=aᵢ₋₁+(i-1)
【算法代码】
#include <bits/stdc++.h> using namespace std; int cur=1,sum=0; int main() { int n; cin>>n; for(int i=1; i<=n; i++) { sum+=cur; cur+=i; } cout<<sum<<endl; return 0; } /* in:6 out:41 */
【参考文献】
https://blog.csdn.net/hnjzsyjyj/article/details/161994231
https://blog.csdn.net/hnjzsyjyj/article/details/161995534
https://blog.csdn.net/liunian_curry/article/details/139721281
