posts - 17,  comments - 0,  trackbacks - 0
Simple calculations

Description

There is a sequence of n+2 elements a0, a1, ..., an+1 (n <= 3000, -1000 <= ai <=1000). It is known that ai = (ai-1 + ai+1)/2 - ci for each i=1, 2, ..., n. 
You are given a0, an+1, c1, ... , cn. Write a program which calculates a1.

Input

The first line of an input contains an integer n. The next two lines consist of numbers a0 and an+1 each having two digits after decimal point, and the next n lines contain numbers ci (also with two digits after decimal point), one number per line.

Output

The output file should contain a1 in the same format as a0 and an+1.

Sample Input

1 50.50 25.50 10.15 

Sample Output

27.85
题意:由公式ai = (ai-1 + ai+1)/2 - ci ;
 给出 a0 an+1 和 c1,c2,c3......ci,求出a1
 
方法:
 2an = an-1 + an+1 - 2cn ;等价于 
an+1 - 
an = an - an-1 + 2cn 
 2an-1 = an-2 + an - 2cn-1 ;等价于 
an - 
an-1 = an-1 - an-2 + 2cn 
 . . . .
 . . . . 
 . . . .

2a3
a2
a4
- 2c3 ;等价于
a4 - 
a3 = a3 - a2 + 2c3 


2a2
a1
a3
- 2c2 ;等价于
a3 - 
a2 = a2 - a1 + 2c2 
 2a1 = a0 + a2 - 2c1
等价于 
a2 - 
a1 = a1 - a0 + 2c1
相加得出:
 an+1 - a1 = an- a0 + 2(c1+c2+c3+....+cn)
因为

an 
还不知道,所以要转化:
an+1 - an = a1- a0 + 2(c1+c2+c3+..............+cn)
 an - an-1 = a1- a0 + 2(c1+c2+c3+..........+cn-1)
 an-1 - an-2 = a1- a0 + 2(c1+c2+c3+....+cn-2)
 . . . . . . .
 . . . . . . .
 . . .. . . . . 

a3 - a2 = a2- a1 + 2(c1 + 

c2)
 a2 - a1 = a1- a0 + 2c1
相加化简得:
 an+1 - a1= a1- a0 +2( nc1 + (n-1)*c2 + (n-2)c3 +.....+2cn-1+cn )
化简得:
 a1=(an+1 +na0-2( nc1 + (n-1)*c2 + (n-2)c3 +.....+2cn-1+cn ))/(n+1)
 1 #include <cstdlib>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 float c[3001],a[3002];
 6 int main(int argc, char *argv[])
 7 {
 8     int n,i,j; float cc=0;
 9     cin>>n;
10     cin>>a[0]>>a[n+1];
11     for(i=1;i<=n;i++)
12     {
13         cin>>c[i];
14         cc+=(n-i+1)*c[i];
15     }
16     a[1]=(a[n+1]+n*a[0]-2*cc)/(n+1);
17     printf("%.2f",a[1]);    
18     system("PAUSE");
19     return EXIT_SUCCESS;
20 }
21 
posted on 2011-10-28 22:51 刘聪 阅读(126) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理