The problem
There is a sequence of n+2 elements a0, A1,..., An+1 (n <=, -1000 <= AI 1000). It is known this 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 was the number of test cases, followed by a blank line.
For each test case, the first line of a input file contains an integer n. The next lines consist of numbers a0 and an+1 each have a digits after decimal point, and the next n lines contain Numbers CI (also with the digits after decimal point), one number per line.
Each test case is separated by a.
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 the consecutive test cases.
Sample Input
1150.5025.5010.15
Sample Output
27.85
Test instructions: Given the values of a[0], and a[n+1], and C[1]~c[n] , have a formula
A[i] = (A[i-1] + a[i+1])/2-c[i];
And then ask for the value of a[1].
Derivation according to the formula:
A[1] = n*a[0] + a[n+1] -2*n*c[1]-(n-1) *c[2]--->>>-2*1*c[n];
A[1] It's OK.
Code:
#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>using namespaceStd;DoubleC[100010];int Main(){ intT; DoubleA0,An; scanf("%d",&T); while(T--) { DoubleN; scanf("%LF",&N); scanf("%LF",&A0); scanf("%LF",&An); for(intI=1;I<=N;I++) { scanf("%LF",&C[I]); } DoubleSum= 0;Sum=N*A0+An; for(intI=1;I<=N;I++) {Sum=Sum- 2.0 *(N-I+1) *C[I]; }Sum=Sum/ (N+1); printf("%.2lf\ n",Sum); if(T!=0)cout<<Endl; } return 0;}
UVA Simple Calculations (mathematical derivation)