UVA 1001 Simple calculations

这个题是典型的数学递推题,把求a1的公式算出来,这道题便解决了。

由2ai = ai–1 + ai+1 – 2ci,将i = 1,2,3,4……n带入,得:

   2a1 = a0 + a2 - 2c1

   2a2 = a1 + a3 - 2c2

   2a3 = a2 + a4 - 2c3

   …… ……   ……

   2an = an-1 + an+1 - 2cn,

然后保留第一个式子i, 并从第一个式子往下加,又得到n个式子:

   2a1 = a0 + a 2 - 2c1

   a1 + a2 = a0 + a3 - 2(c1 + c2)

   a1 + a3 = a0 + a4 - 2(c1 + c2 + c3)

   …… …… …… ……

   a1 + an = a0 + a - 2(c1 + c2 + c3 + …… + cn)

n个式子相加,得:

(n + 1)a1 = na+ an+1 - 2(nc1 + (n-1)c2 + (n-2)c3 + …… + cn),便可求出答案.


#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <numeric>
#include <algorithm>
#include <functional>
#define  inf 0x3f3f3f3f
#define  INF 0x3f3f3f3f3f3f3f3fLL
#define  pi  acos(-1.0)
#define  eps 1e-8
//#define  spongewxy_code
using namespace std;

int main() {

#ifdef spongewxy_code
    freopen("aa.in", "r", stdin);
    freopen("bb.out", "w", stdout);
#endif
    int T, n;
    double ans, m, x, y;
    int kcase = 0;
    cin >> T;
    while(T--) {
        if(kcase) {
            puts("");
        }
        kcase++;
        cin >> n;
        cin >> x >> y;
        ans = 0.0;
        ans += (n * x + y);
        for(int i = 1; i <= n; ++i) {
            cin >> m;
            ans -= 2 * (n - i + 1) * m;
        }
        printf("%.2lf\n", ans / (n + 1.0));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值