Equations
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2086
——每天在线,欢迎留言谈论。
题目大意:
有如下方程:Ai = (Ai-1 + Ai+1)/2 - Ci (i = 1, 2, 3, .... n)。
若给出A0, An+1, 和 C1, C2, .....Cn。
求 A1 。
思路:
多写几个例子,找规律推导(抄的)。
感想:
老啦,老啦,不行了。
Java AC代码:
1 import java.util.Scanner; 2 import java.math.*; 3 4 public class Main { 5 static Scanner scn = new Scanner(System.in); 6 static final int MAXN = 3001; 7 static double[] c = new double[MAXN]; 8 public static void main(String[] args) { 9 int n; 10 double a, b, answer; 11 while (scn.hasNext()) { 12 n = scn.nextInt(); 13 a = scn.nextDouble(); 14 b = scn.nextDouble(); 15 for (int i = 1; i <= n; i++) { 16 c[i] = scn.nextDouble(); 17 } 18 answer = n * a + b; 19 for (int i = n; i >= 1; i--) { 20 answer -= 2 * i * c[n - i + 1]; 21 } 22 answer /= (n + 1); 23 System.out.println(String.format("%.2f", answer)); 24 } 25 System.exit(0); 26 } 27 /** 28 * 因为:Ai=(Ai-1+Ai+1)/2 - Ci, 29 * A1=(A0 +A2 )/2 - C1; 30 * A2=(A1 + A3)/2 - C2 , ... 31 * => A1+A2 = (A0+A2+A1+A3)/2 - (C1+C2) 32 * 2[(A1+A2)+(C1+C2)] = A0+A2+A1+A3; 33 * A1+A2 = A0+A3 - 2(C1+C2); 34 * => A1+A2 = A0+A3 - 2(C1+C2) 35 * 同理可得: 36 * A1+A1 = A0+A2 - 2(C1) 37 * A1+A2 = A0+A3 - 2(C1+C2) 38 * A1+A3 = A0+A4 - 2(C1+C2+C3) 39 * A1+A4 = A0+A5 - 2(C1+C2+C3+C4) 40 * ... 41 * A1+An = A0+An+1 - 2(C1+C2+...+Cn) 42 *----------------------------------------------------- 左右求和 43 *(n+1)A1+(A2+A3+...+An) = nA0 +(A2+A3+...+An) + An+1 - 2(nC1+(n-1)C2+...+2Cn-1+Cn) 44 * 45 * => (n+1)A1 = nA0 + An+1 - 2(nC1+(n-1)C2+...+2Cn-1+Cn) 46 * 47 * => A1 = [nA0 + An+1 - 2(nC1+(n-1)C2+...+2Cn-1+Cn)]/(n+1) 48 */ 49 }
2017-08-11 17:46:51