[Zhengqing] [1841] So easy! Mom Never Has to Worry About My Math Again!

Problem Description

so easy!麻麻再也不用担心我的数学了!
Time limit: 1 SecMemory limit: 128 MB

Description

Let’s do a really easy math problem, so Mom no longer has to worry about us failing math.
Now you are given an equation A[i] = (A[i-1]+A[i+1])/2 - c[i](1 <= i <= n);, and then you are given A[0] and A[n+1]
Find A[1] = ?.

Input

Contains multiple test cases.
For each instance, the first line contains a positive integer n(1 <= n <= 3000), the second line contains two real numbers A[0], A[n+1], and each of the following lines contains a real number c[i](1 <= i <= n).

Output

For each test case, output the required a1 in one line (keep 2 decimal places).

Sample Input

1
2
3
4
5
6
1  
50 25  
10  
2  
50 25  
10 20

Sample Output

1
2
27.50  
15.00

Problem Analysis

This is a problem that takes some thought.
According to the formula:
we can obtain:
Therefore:

…

Thus we can get

From this, the result can be obtained directly

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include<stdio.h>
double a[3020];
int main() {
    int n;
    while(scanf("%d",&n)!=EOF) {
        double f0,fn1;
        scanf("%lf %lf",&f0,&fn1);
        for(int i=1; i<=n; i++)
            scanf("%lf",&a[i]);
        double sum=0;
        for(int i=1; i<=n; i++)
            sum+=2*(n-i+1)*a[i];
        printf("%.2lf\n",(fn1+n*f0-sum)/(n+1));
    }
    return 0;
}

Problem link: ZZULI [1841] so easy! Mom no longer has to worry about my math!

Loading...