1047. Simple Calculations

1047. Simple Calculations

Time Limit: 1.0 second
Memory Limit: 16 MB
There is a sequence of  N + 2 elements  a 0a 1, …,  a N+1 (1 ≤  N ≤ 3000, −2000 ≤  a i ≤ 2000). It is known that
a i = ( a i−1 +  a i+1)/2 −  c i
for each  i = 1, 2, …,  N.
You are given  a 0a N+1c 1, …,  c N. Write a program which calculates  a 1.

Input

The first line contains an integer  N. The next two lines consist of numbers  a 0 and  a N+1 each having two digits after decimal point, and the next  N lines contain numbers  c i (also with two digits after decimal point), one number per line.

Output

Output  a 1 in the same format as  a 0 and  a N+1.

Sample

input output
1
50.50
25.50
10.15
27.85



根据原式可以得

a[n+1]-a[n]=a[n]-a[n-1]+2*c[n]①

设S[n]=c[1]+c[2]+…+c[n]

对①式叠加相消可以得到a[n+1]-a[1]=a[n]-a[0]+2*S[n]

整理得a[n+1]-a[n]=a[1]-a[0]+2*S[n]②

对②式叠加相消可得到a[n+1]-a[1]=n*(a[1]-a[0])+2*(S[1]+S[2]+…+S[n])

#include <iostream>
using namespace std;

int main()
{
	int i, n;
	double a_0, a_n1, a_1, c[3000], s = 0, temp = 0;
	cin>>n>>a_0>>a_n1;
	for (i=0; i<n; i++)
		cin>>c[i];
	for (i=0; i<n; i++)
	{
		temp += c[i];
		s += temp;
	}
	a_1 = (a_n1 + n * a_0 - 2 * s) / (n + 1);
	printf("%.2f\n", a_1);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值