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. Matplotlib基础图形之散点图

    Matplotlib基础图形之散点图 散点图特点: 1.散点图显示两组数据的值,每个点的坐标位置由变量的值决定 2.由一组不连续的点组成,用于观察两种变量的相关性(正相关,负相关,不相关) 3.例如: ...

  2. [android 应用开发]android 分层

    1 应用层, 2 应用框架层(框架是所有开发人员共同使用和遵守的约定) 3 系统运行库层 4 linux内核层

  3. 【JavaScript 3—基础知识点】:运算符

    导读:其实看到这个运算符的学习,很有一种熟悉感,因为在总体看来,和之前的C++有很多类似的地方,但当时觉得简单,没有总结.所以,这次一定得总结了.其实,知识的罗列,基础的积累,在学习中也很重要. 一. ...

  4. 九度oj 题目1020:最小长方形

    题目描述:     给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个最小的长方形框将所有点框在内.长方形框的边分别平行于x和y坐标轴,点落在边上也算是被框在内. 输入: 测试输入 ...

  5. Spring boot 中 Spring Security 使用改造5部曲(转)

    文章的内容有点长,也是自己学习Spring security的一个总结.如果你从头看到尾,我想你对Spring Security的使用和基本原理应该会有一个比较清晰的认识. 如果有什么理解不对的地方, ...

  6. 中国余数定理 1(codevs 3040)

    题目描述 Description 摘自算法导论...... 找出第k个被3,5,7除的时候,余数为2,3,2的数: 输入描述 Input Description 一个数k. 输出描述 Output D ...

  7. ZOJ - 3816 Generalized Palindromic Number dfs

    Generalized Palindromic Number Time Limit: 2 Seconds                                     Memory Limi ...

  8. mongo安装-docker

    拉取官方镜像(可以自己使用dockerFile构建): docker search mongo //查询Docker Hub 上的mongo镜像 docker pull mongo // 拉取官方镜像 ...

  9. python练习之-计算器

    学习以堆栈模式编写-计算器 堆栈特点:先进后出, 如下: #!/opt/python3/bin/python3 # Author: yong import re def is_symbol(eleme ...

  10. codevs——1690 开关灯

    1690 开关灯 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description YYX家门前的街上有N( ...