A1 = ? Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6492 Accepted Submission(s): 4038
Problem Description 有如下方程:Ai = (Ai-1 + Ai+1)/2 - Ci (i = 1, 2, 3, .... n). 若给出A0, An+1, 和 C1, C2, .....Cn. 请编程计算A1 = ?
Input 输入包括多个测试实例。 对于每个实例,首先是一个正整数n,(n <= 3000); 然后是2个数a0, an+1.接下来的n行每行有一个数ci(i = 1, ....n);输入以文件结束符结束。
Output 对于每个测试实例,用一行输出所求得的a1(保留2位小数).
Sample Input 1 50.00 25.00 10.00 2 50.00 25.00 10.00 20.00
Sample Output 27.50 15.00
Source |
1 /* 2 Ai=(Ai-1+Ai+1)/2 - Ci, 3 A1=(A0 + A2)/2 - C1; 4 A2=(A1 + A3)/2 - C2 , ... 5 => A1+A2 = (A0+A2+A1+A3)/2 - (C1+C2) 6 2[(A1+A2)+(C1+C2)] = A0+A2+A1+A3; 7 A1+A2 = A0+A3 - 2(C1+C2); 8 => A1+A2 = A0+A3 - 2(C1+C2) 9 同理可得: 10 A1+A1 = A0+A2 - 2(C1) 11 A1+A2 = A0+A3 - 2(C1+C2) 12 A1+A3 = A0+A4 - 2(C1+C2+C3) 13 A1+A4 = A0+A5 - 2(C1+C2+C3+C4) 14 ... 15 A1+An = A0+An+1 - 2(C1+C2+...+Cn) 16 ----------------------------------------------------- 左右求和 17 (n+1)A1+(A2+A3+...+An) = nA0 +(A2+A3+...+An) + An+1 - 2(nC1+(n-1)C2+...+2Cn-1+Cn) 18 19 => (n+1)A1 = nA0 + An+1 - 2(nC1+(n-1)C2+...+2Cn-1+Cn) 20 21 => A1 = [nA0 + An+1 - 2(nC1+(n-1)C2+...+2Cn-1+Cn)]/(n+1) 22 */ 23 //!!!!用cin和cout会超时 24 #include <iostream> 25 #include <cstdio> 26 using namespace std; 27 double a[3010],c[3010]; 28 int main() 29 { 30 int n; 31 while(~scanf("%d",&n)){ 32 double sum=0; 33 scanf("%lf %lf",&a[0],&a[n+1]); 34 for(int i=n;i>=1;i--){ 35 scanf("%lf",&c[i]); 36 sum+=i*c[i]; 37 } 38 a[1]=(a[0]*n+a[n+1]-2*sum)/(n+1); 39 printf("%.2f\n",a[1]); 40 } 41 return 0; 42 }