HDU-2086 A1 = ?

A1 = ?

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3244    Accepted Submission(s): 2054


Problem Description
有如下方程:A i = (A i-1 + A i+1)/2 - C i (i = 1, 2, 3, .... n).
若给出A 0, A n+1, 和 C 1, C 2, .....C n.
请编程计算A 1 = ?
 

 

Input
输入包括多个测试实例。
对于每个实例,首先是一个正整数n,(n <= 3000); 然后是2个数a 0, a n+1.接下来的n行每行有一个数c i(i = 1, ....n);输入以文件结束符结束。
 

 

Output
对于每个测试实例,用一行输出所求得的a1(保留2位小数).
 

 

Sample Input
1 50.00 25.00 10.00 2 50.00 25.00 10.00 20.00
 

 

Sample Output
27.50 15.00
/*
递推题:
	逆着写几项就总结出规律了
	我推出的公式为:
		a[1]=1/(n+1)*(a[n+1]+n*a[0]-2*c[n]-4*c[n-1]-8*c[n-2]...-pow(2,n)*c[1]);
*/
//代码一:
#include<stdio.h>

int main()
{
	int n,i,k;
	float a[3005],c[3005];
	float sum;
	while(~scanf("%d",&n))
	{
		scanf("%f%f",&a[0],&a[n+1]);
		for(i=1;i<=n;++i)
			scanf("%f",&c[i]);
		k=2;
		sum=0;
		for(i=n;i>0;--i)
		{
			c[i]*=k;
			k+=2;
			sum+=c[i];
		}
		printf("%.2f\n",1.0/(n+1)*(a[n+1]+n*a[0]-sum));
	}
	return 0;
}

//代码二:---参考网上的代码
/*
因为:Ai=(Ai-1+Ai+1)/2 - Ci, 
      A1=(A0  +A2  )/2 - C1;
      A2=(A1  +  A3)/2 - C2 , ...
=>    A1+A2 = (A0+A2+A1+A3)/2 - (C1+C2)
=>    A1+A2 =  A0+A3 - 2(C1+C2) 
同理可得:
      A1+A1 =  A0+A2 - 2(C1) 
      A1+A2 =  A0+A3 - 2(C1+C2)
      A1+A3 =  A0+A4 - 2(C1+C2+C3)
      A1+A4 =  A0+A5 - 2(C1+C2+C3+C4)
      ...
      A1+An = A0+An+1 - 2(C1+C2+...+Cn)
----------------------------------------------------- 左右求和
     (n+1)A1+(A2+A3+...+An) = nA0 +(A2+A3+...+An) + An+1 - 2(nC1+(n-1)C2+...+2Cn-1+Cn)

=>   (n+1)A1 = nA0 + An+1 - 2(nC1+(n-1)C2+...+2Cn-1+Cn)

=>   A1 = [nA0 + An+1 - 2(nC1+(n-1)C2+...+2Cn-1+Cn)]/(n+1)
*/

#include<stdio.h>
int main()
{
	int t,i,n;
	double a,b,c,a1,sum1,sum2;
	while(scanf("%d",&n)!=EOF)
	{
		t=n;
		scanf("%lf%lf",&a,&b);
		sum1=t*a+b;
		sum2=0;
		for(i=1;i<=n;i++)
		{
			scanf("%lf",&c);
			sum2=sum2+t*c;
			t--;
		}
		a1=(sum1-2*sum2)/(n+1);
		printf("%.2f\n",a1);
	}
	return 0;
}

  

你可能感兴趣的:(HDU)