问题链接:http://www.dotcpp.com/oj/problem1168.html
问题简述:有一个n+2个元素a[0], a[1], ..., a[n+1] (n <= 3000, -1000 <= a[i] <=1000)构成的数列.
已知对i=1, 2, ..., n有a[i] = (a[i-1] + a[i+1])/2 - c[i].
给定a0, a[n+1], c[1], ... , c[n]. 写一个程序计算a[1].
解题思路:计算递推公式,详见http://www.dotcpp.com/blog/4442.html
解题代码:
#include <iostream>
#include <cstdio>
#define N 3002
using namespace std;
int main(){
double a0, an_1, c[N] = {0};
int n;
cin >> n >> a0 >> an_1;
double sum = 0.0;
for(int i = 1; i <= n; i++){
cin >> c[i];
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= i; j++){
sum += c[j];
}
}
printf("%.2lf\n", (1.0 * n * a0 + an_1 - 2.0 * sum) / (n + 1));
return 0;
}