hdu 2086 A1 = ?(数学题)

分析:(来自大神的博客)

因为:Ai=(Ai-1+Ai+1)/2 - Ci, 
      A1=(A0  +A2  )/2 - C1;
      A2=(A1  +  A3)/2 - C2 , ...
=>    A1+A2 = (A0+A2+A1+A3)/2 - (C1+C2)
=>    A1+A2 =  A0+A3 - 2(C1+C2) 
同理可得:
      A1+A1 =  A0+A2 - 2(C1) 
      A1+A2 =  A0+A3 - 2(C1+C2)
      A1+A3 =  A0+A4 - 2(C1+C2+C3)
      A1+A4 =  A0+A5 - 2(C1+C2+C3+C4)
      ...
      A1+An = A0+An+1 - 2(C1+C2+...+Cn)
----------------------------------------------------- 左右求和
     (n+1)A1+(A2+A3+...+An) = nA0 +(A2+A3+...+An) + An+1 - 2(nC1+(n-1)C2+...+2Cn-1+Cn)

=>   (n+1)A1 = nA0 + An+1 - 2(nC1+(n-1)C2+...+2Cn-1+Cn)

=>   A1 = [nA0 + An+1 - 2(nC1+(n-1)C2+...+2Cn-1+Cn)]/(n+1)

代码:

#include<cstdio>
#include<cmath>
using namespace std;

double c[5000];

int main()
{
    int n;
    double a,aa;
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%lf%lf",&a,&aa);
        for(int i=1;i<=n;i++)
        {
            scanf("%lf",&c[i]);
        }
        double ans=0;
        int k=1;
        int nn=n;
        while(n)
        {
            ans=ans+n*c[k];
            n--; 
            k++;
        }

        ans=(nn*a+aa-2*ans)/(nn+1);
        printf("%.2lf\n",ans);
    }
    return 0;
}


你可能感兴趣的:(数学)