递归式移项得Ai+1 = 2Ai + 2Ci - Ai-1;
1.A2 = 2A1 + 2C1 - A0;
2.A3 = 2A2 + 2C2 - A1;
.
.
.
n.An+1 = 2An + 2Cn - An-1;
如此的1式带入2式,之后的2式带入3式,直到n式被更新为An+1 = n+1A1 + (2nC1 + 2(n-1)C2 + ... + 2Cn - nA0,只剩下A1未知了。
代码:
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #include <iomanip> using namespace std; int n; double c,a0,an1,sum; int main() { cin>>n; cin>>a0>>an1; sum += n * a0 + an1; for(int i = 0;i < n;i ++) { cin>>c; sum -= (n - i) * c * 2; } sum /= n + 1; printf("%.2f",sum); }
如果觉得有帮助,点个推荐啦~