数学题 HDOJ——2086 简单归纳
哎 真的是懒得动脑子还是怎么滴。。。
题目如下
Problem Description
有如下方程:Ai = (Ai-1 + Ai+1)/2 - Ci (i = 1, 2, 3, .... n).
若给出A0, An+1, 和 C1, C2, .....Cn.
请编程计算A1 = ?
若给出A0, An+1, 和 C1, C2, .....Cn.
请编程计算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<stdio.h> #include<string.h> #include<vector> #include<cmath> using namespace std; int n;double a,b,c; int main() { while(scanf("%d",&n)!=EOF) { double ans=0; scanf("%lf%lf",&a,&b); ans+=n*a+b; for(int i=n;i>=1;i--) { scanf("%lf",&c); ans-=2*i*c; } printf("%.2f\n",ans/(n+1)); } return 0; }