Description
有一个包括n+2个元素的数列a0, a1, ..., an+1 (n <= 3000, -1000 <= ai <=1000)。它们之间满足ai = (ai-1 + ai+1)/2 - ci (i=1, 2, ..., n)。如今给出a0, an+1, c1, ... , cn.。请编敲代码计算出a1。
Input
输入的第一行是一个整数n。

接下去的两行各自是a0和an+1,(精确到小数点后两位)再接下去的n行是ci(精确到小数点后两位)。每一个数字占一行。

Output
输出a1 ,其格式与 a0 同样。
Sample Input
1

50.50

25.50

10.15
Sample Output
27.85

解题思路:

一開始想用递归来求,但是数据挺大的,n<=3000,所以便想找找当中的规律,看看能不能列出一个算式让a[1]用a[0],a[n+1]和c[1~n]来表示。

首先能够列出下面算式:
2 * a[1] = a[0] + a[2] - 2 * c[1]

2 * a[2] = a[1] + a[3] - 2 * c[2]
......
2 * a[n] = a[n-1] + a[n+1] - 2 * c[n]

等式两边分别相加可得:
2 * (a[1] + ... + a[n]) = (a[0] + ... + a[n-1]) + (a[2] + ... + a[n+1]) - 2 * (c[1] + ... + c[n])

移项后得:
a[1] + a[n] = a[0] + a[n+1] - 2 * (c[1] + ... + c[n])

让下标n从1~n,列出以上算式:
a[1] + a[1] = a[0] + a[2] - 2 * c[1]

a[1] + a[2] = a[0] + a[3] - 2 * (c[1] + c[2])

a[1] + a[n] = a[0] + a[n+1] - 2 * (c[1] + ... + c[n])

等式两边分别相加后得:
(n + 1) * a[1] = n * a[0] + a[n+1] - 2 * (c[1] + (c[1] + c[2]) + ... + (c[1] + c[2] + ... +c[n])) 

AC代码:

#include<stdio.h>
#define MAX_NUM 3005
int main()
{
double a[MAX_NUM], c[MAX_NUM];
int n;
scanf("%d", &n);
scanf("%lf%lf", &a[0], &a[n+1]);
for(int i = 1; i <= n; i++)
scanf("%lf", &c[i]);
int j = 1;
double sum_1 = 0, sum_2 = 0;
for(int i = 1; i <= n ; i++)
{
for(; j <= i; j++)
{
sum_1 += c[j];
}
sum_2 += sum_1;
}
a[1] = (n * a[0] + a[n + 1] - 2 * sum_2) / (n + 1);
printf("%.2lf\n", a[1]);
return 0;
}

Simple calculations的更多相关文章

  1. 【POJ】【2601】Simple calculations

    推公式/二分法 好题! 题解:http://blog.csdn.net/zck921031/article/details/7690288 这题明显是一个方程组……可以推公式推出来…… 然而这太繁琐了 ...

  2. UVA - 10014 - Simple calculations (经典的数学推导题!!)

    UVA - 10014 Simple calculations Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

  3. poj 2601 Simple calculations

    Simple calculations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6559   Accepted: 32 ...

  4. uva 10014 Simple calculations

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  5. UVa10820 Send a Table[欧拉函数]

    Send a TableInput: Standard Input Output: Standard Output When participating in programming contests ...

  6. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  7. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 排序、筛选、分页以及分组

    Sorting, filtering, paging, and grouping 7 of 8 people found this helpful By Tom Dykstra The Contoso ...

  8. UVA 10820 - Send a Table 数论 (欧拉函数)

    Send a Table Input: Standard Input Output: Standard Output When participating in programming contest ...

  9. uva 10820 (筛法构造欧拉函数)

    send a table When participating in programming contests, you sometimes face the following problem: Y ...

随机推荐

  1. PYDay3-初识python

    Python 种类 c.j.iron.ruby等,主要有三类:cpython.xxxpython.pypy 种类繁多我们精通一种即可 编译流程: py代码->字节码->机器码->计算 ...

  2. jquery map.js

    (function ($) { HashMap = function () { var index = 0; var content = ''; var keyV = new Array(); var ...

  3. [转]pickle python数据存储

    python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件 ...

  4. centos dhcp 服务器搭建 多vlan

    centos dhcp 服务器搭建   多vlan centos 6.5   版本     /etc/dhcp/dhcpd.conf         服务器配置文件 /etc/rc.d/init.d/ ...

  5. 【C#】最后总结

    导读:要想收获,就逃不开总结.一直拖着拖着,再也无法忍受了.应该说是又学习迷茫了,所以,我要总结.一直都觉得自己总结不出来,或者是看了别人的优秀总结,心里就打鼓,不敢下笔.现在,化用一下:但热闹是他们 ...

  6. SPOJ - DQUERY 主席树求区间有多少个不同的数(模板)

    D-query Time Limit: 227MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Submit Status ...

  7. tomcat在centos6+上的自启动脚本

    #!/bin/bash # # tomcat startup script for the Tomcat server # # chkconfig: 345 80 20 # description: ...

  8. hdu6060[贪心+dfs] 2017多校3

    /* hdu6060[贪心+dfs] 2017多校3*/ #include <bits/stdc++.h> using namespace std; typedef long long L ...

  9. Python之虚拟机操作:利用VIX二次开发,实现自己的pyvix(系列一)成果展示和python实例

    在日常工作中,需要使用python脚本去自动化控制VMware虚拟机,现有的pyvix功能较少,而且不适合个人编程习惯,故萌发了开发一个berlin版本pyvix的想法,暂且叫其OpenPyVix.O ...

  10. 【Luogu】P2912牧场散步(TarjanLCA)

    题目链接 老天……终于碰上一个除了模板之外的LCA题了 这道题用Tarjan来LCA.树上两个点的路径是唯一的,所以钦定一个根,两点间的路径就是两点到根的路径减去双倍的公共祖先到根的路径.大概很好理解 ...