程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVA 10014 Simple calculations

UVA 10014 Simple calculations

編輯:C++入門知識

 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
1150.5025.5010.15Sample Output
27.85

題意:。。根據題目給的公式。輸入了a0, a(n + 1)..和n個Ci。要根據公式推出a1.。。

就是推導公式。。。

將i等於1、 2 、3 ...n帶入原式子。。。

得到


 2a1 = a0 + a2 - 2c1

   2a2 = a1 + a3 - 2c2

   2a3 = a2 + a4 - 2c3

   …… ……   ……

   2an = an-1 + an+1 - 2cn,

可以得到 a1 - a0 + 2(c1 +c2 +... +cn) = a(n + 1) - a(n).

在把n = 1, 2, 3 ,4 ,5 ,6...n 。。帶入

得到a1 - a0 + 2(c1) = a2 - a1;

        a1 - a0 + 2(c1 + c2) = a3 - a2;

。。。。

a1 - a0 + 2(c1 + c2 + c3 +...cn) = a(n+1) - a(n);

然後把上式子全部疊加。。可以得到n * (a1 - a0) + 2(n*c1 + (n -1) * c2 + (n -2) * c3+ .... 2*cn - 1 + cn) = a(n + 1) - a1;

這樣除了a1 其他全是已知量了。 - - 好麻煩啊。。。然後根據這個公式求出a1輸出。

 


 

#include <stdio.h>
#include <string.h>

int t;
int n;
double star, end;
double c[3005];
double a1;
int main()
{
    scanf("%d", &t);
    while (t --)
    {
	memset(c, 0 ,sizeof(c));
	scanf("%d", &n);
	    scanf("%lf%lf",&star, &end);
	for (int i = 1; i <= n; i ++)
	    scanf("%lf", &c[i]);
	a1 = (end + n * star);
	for (int i = 1; i <= n; i ++)
	{
	    a1 -= 2 * c[i] * (n + 1 - i);
	}
	a1 /= n + 1;
	printf("%.2lf\n", a1);
	if (t)
	    printf("\n");
    }
    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved