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. luogu1640 [SCOI2010]连续攻击游戏

    二分图匹配,一边是属性值,一边是武器 #include <iostream> #include <cstring> #include <cstdio> using ...

  2. FineMessBox的js依赖导致错误Uncaught ReferenceError: addEvent is not defined

    /**  * COMMON DHTML FUNCTIONS  * These are handy functions I use all the time.  *  * By Seth Banks ( ...

  3. Dialog共通写法(一个button)

    一个button的: package jp.co.hyakujushibank.view import android.app.Dialogimport android.content.Context ...

  4. iOS开发工具篇-AppStore统计工具

    苹果官方的iTunes Connect提供的销售数据统计功能比较弱,例如只能保存最近30天的详细销售数据,界面丑陋, 无法查看App的排名历史变化情况等. 早有一些公司提供了专门的解决方案或工具.这些 ...

  5. iOS学习笔记04-视图切换

    一.视图切换 UITabBarController (分页控制器) - 平行管理视图 UINavigationController (导航控制器) - 压栈出栈管理视图 模态窗口 二.UITabBar ...

  6. 洛谷P2351 [SDOi2012]吊灯 【数学】

    题目 Alice家里有一盏很大的吊灯.所谓吊灯,就是由很多个灯泡组成.只有一个灯泡是挂在天花板上的,剩下的灯泡都是挂在其他的灯泡上的.也就是说,整个吊灯实际上类似于[b]一棵树[/b].其中编号为 1 ...

  7.  为什么上传文件的表单里要加个属性enctype----摘录

    上传文件的表单中<form>要加属性enctype="multipart/form-data",很多人只是死记硬背知道上传表单要这么写,知其然而不知其所以然.那到底为什 ...

  8. COdevs 天梯 水题系列

    1203 判断浮点数是否相等 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题目描述 Description 给出两个浮点数,请你判断这两个浮点数是否相等 输入 ...

  9. NOIP 前夕 模板整理

    归并排序: #include<iostream> #include<cstdio> #include<cstring> using namespace std; ] ...

  10. Java面试题集(三)

    Jdk与jre的区别? Java运行是环境(jre)是将要执行java程序的java虚拟机. Java开发工具包(jdk)是完整的java软件开发包,包含jre,编译器和其他工具如javaDoc,ja ...