标签:style blog http io ar color os sp for
有如下方程:Ai = (Ai-1 + Ai+1)/2 - Ci (i = 1, 2, 3, .... n).
若给出A0, An+1, 和 C1, C2, .....Cn.
请编程计算A1 = ?
思路:
推公式:
首先,可以推出一个公式1:A1+An+2∑[1,n]Ci = A0+A(n+1)
所以:
A1+A1+2∑[1,1]Ci = A0+A2
A1+A2+2∑[1,2]Ci = A0+A3
A1+A3+2∑[1,3]Ci = A0+A4
.....
A1+An+2∑[1,n]Ci = A0+An+1
左侧加起来 = 右侧加起来,化简,得:
(n+1)A1+2(....) = nA0+A(n+1)
代码:
double c[3005]; double ax,ay; int main(){ int n; while(cin>>n){ cin>>ax>>ay; c[0]=0.0; rep(i,1,n){ double x; cin>>x; c[i]=c[i-1]+x; } double res1=0.0; rep(i,1,n) res1+=(c[i]); res1*=2; printf("%.2lf\n",(n*ax+ay-res1)/(n+1)); } }
标签:style blog http io ar color os sp for
原文地址:http://www.cnblogs.com/fish7/p/4138123.html