您的位置:首页 > 其它

uva10014(数学)

2013-05-30 10:58 393 查看
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=12&problem=955&mosmsg=Submission+received+with+ID+10538028

10014 - Simple calculations

Time limit: 3.000 seconds

Simple
calculations


The Problem

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.

The Input

The first line is the number of test
cases, followed by a blank line.

For each test case, the first line of an input file 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.

Each test case will be separated by a single line.

The Output

For each test case, the output file
should contain a1 in the same format as a0
and an+1.

Print a blank line between the outputs for two consecutive test
cases.

Sample Input

1

1
50.50
25.50
10.15


Sample Output

27.85
题意:出处http://www.byywee.com/page/M0/S774/774446.html

给出一个数列ci(1<=ci<=n),然后给出数列ai中的a0和a(n+1),并给出一个公式ai = ( a(i-1) + a(i+1) ) / 2 - ci。求a1。

做了很长时候,也没弄对,最后看的别人的解题呈报。。。。。

由给出公式得:2ai = a(i-1) + a(i+1) -2ci ,依次使i=1,2......n,写出n个式子,把它们相加,

获得:a1 + an = a0 + a - 2(c1 + …… + cn), 再使c的下标i=1......n,写出n个式子,把他们相加。

获得:(n + 1)a1 = na0 + an+1 - 2(nc1 + (n-1)c2 + (n-2)c3 + …… + cn)。

自己也推到:a1 + an = a0 + a - 2(c1 + …… + cn), 后面就不会做了。用了种不负责任的态度提交了下。

#include<stdio.h>

#include<string.h>

#define maxn 3010

double a[maxn],c[maxn];

int main()

{

 int t;

 scanf("%d",&t);

 while(t--)

 {

  int n;

  scanf("%d",&n);

  memset(a,0,sizeof(a));

  scanf("%lf%lf",&a[0],&a[n+1]);

  int i;

  double sum=0.0;

  for(i=0;i<n;i++)

  {

   scanf("%lf",&c[i]);

   sum+=(n-i)*c[i];

  }

  printf("%.2lf\n",(a[n+1]+n*a[0]-2*sum)/(n+1));

  if(t)

   printf("\n");

 }

 return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: