Simple calculations
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 shoshould 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.15Sample Output
27.85
Question :.. Based on the formula given by the question. Input a0, a (n + 1)... and n Ci. According to the formula, a1...
Is to deduce the formula...
Add I to the original formula...
Get
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 ).
Put n = 1, 2, 3, 4, 5, 6... n .. Import
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 );
Then, all the above sub-statements are superimposed .. We can obtain n * (a1-a0) + 2 (n * c1 + (n-1) * c2 + (n-2) * c3 + .... 2 * cn-1 + cn) = a (n + 1)-a1;
In this way, all the others except A1. --It's troublesome... Then the a1 output is obtained based on this formula.
#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;}