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> #include <string.h> int t; int n; double star, end; double c[3005]; double a1; int main() { scanf("%d", &t); while (t --) { memset(c, 0 ,sizeof(c)); scanf("%d", &n); scanf("%lf%lf",&star, &end); for (int i = 1; i <= n; i ++) scanf("%lf", &c[i]); a1 = (end + n * star); for (int i = 1; i <= n; i ++) { a1 -= 2 * c[i] * (n + 1 - i); } a1 /= n + 1; printf("%.2lf\n", a1); if (t) printf("\n"); } return 0; }