The Problem
There is a sequence of n+2 elements a0, a1,…, an+1 (n <= 3000; -1000 <= ai 1000). It is known that ai = (ai–1 + ai+1)/2 – ci for each i=1, 2, ..., n. You are given a0, an+1, c1, ... , cn. Write a program which calculates a1.
The Input
The first line is the number of test cases, followed by a blank line.
For each test case, the first line of an input file contains an integer n. The next two lines consist of numbers a0 and an+1 each having two digits after decimal point, and the next n lines contain numbers ci (also with two digits after decimal point), one number per line.
Each test case will be separated by a single line.
The Output
For each test case, the output file should contain a1 in the same format as a0 and an+1.
Print a blank line between the outputs for two consecutive test cases.
Sample Input
1150.5025.5010.15
Sample Output
27.85
题意:
根据题目给的公式。
输入了a0, a(n + 1)……和n个Ci。要根据公式推出a1。
解析:
就是推导公式题
将i等于1、 2 、3 ...n带入原式子。。。
得到
2a1 = a0 + a2 - 2c1
2a2 = a1 + a3 - 2c2
2a3 = a2 + a4 - 2c3
……
2an = an-1 + an+1 - 2cn,
可以得到 a1 - a0 + 2(c1 +c2 +... +cn) = a(n + 1) - a(n).
再把n = 1, 2, 3, 4, 5, 6……n 带入
得到
a1 - a0 + 2(c1) = a2 - a1
a1 - a0 + 2(c1 + c2) = a3 - a2;
a1 - a0 + 2(c1 + c2 + c3 +...cn) = a(n+1) - a(n);
然后把上式子全部叠加。
可以得到
n * (a1 - a0) + 2(n*c1 + (n -1) * c2 + (n -2) * c3+ …… 2*cn - 1 + cn) = a(n + 1) - a1;
这样除了a1 其他全是已知量了,然后根据这个公式求出a1输出。
#include<stdio.h>
const int N = 3010;
double c[N];
int main() {
int t;
int n;
double first,end;
while(scanf("%d",&t) != EOF) {
while(t--) {
scanf("%d",&n);
scanf("%lf",&first);
scanf("%lf",&end);
for(int i=1;i<=n;i++) {
scanf("%lf",&c[i]);
}
double sum = n*first + end;
for(int i=0;i<n;i++) {
sum -= 2.0*(n-i)*c[i+1];
}
printf("%0.2lf\n",sum/(n+1));
if(t != 0)
printf("\n");
}
}
return 0;
}