1 条题解
-
0
练11.3 等差数列末项计算
解题思路
公差 d=a2-a1,第 n 项为 a1+(n-1)d。
正确性说明
从第一项到第 n 项经过 n-1 次相同公差,累加得到公式。
易错点
n=1 时仍输出 a1;公差可以为负。
复杂度
本题输入规模固定,时间 O(1),额外空间 O(1)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long a, b, n; cin >> a >> b >> n; cout << a + (n - 1) * (b - a) << "\n"; return 0; }
- 1