Uva--10014 (数学)
2014-06-10 22:06:07
题意&思路:ai = (ai-1 + ai+1) / 2 - ci 将i从1到n列出n个等式,全部相加,得到:a1 + an = a0 + an+1 - 2(c1+c2+c3+......+cn).
关键步骤:将上述式子的n从1开始到n列出你n个式子,(如第二个式子为:a1 + a2 = a0 + a3 - 2(c1 + c2).)
全部相加得到:(n+1)*a1 = n*a0 + an+1 - 2( nc1 + (n-1)c2 + (n-2)c3+....+(n-i+1)ci+....+cn),可以解出a1
#include <cstdio> #include <iostream> using namespace std; int n,Case; double a0,an1,c[3005],sum; int main(){ scanf("%d",&Case); while(Case--){ sum = 0; scanf("%d",&n); scanf("%lf %lf",&a0,&an1); for(int i = 1; i <= n; ++i){ scanf("%lf",&c[i]); sum += c[i] * (n - i + 1); } printf("%.2lf\n",(n * a0 + an1 - 2 * sum) / (n + 1)); if(Case) puts(""); } return 0; }