Simple Calculation

Simple Calculation

Num : 7

Time Limit : 1000ms

Memory Limit : 65536K

description

有一个包含n+2个元素的序列(a[0],a[1],…,a[n+1])。我们知道,对于任意的i(i=1,2,...,n),满足a[i]=(a[i-1]+a[i+1])/2-c[i]。给定a[0],a[n+1],c[0],c[1],...c[n],你的任务是写一个程序求出a[1]。</span>

input

输入包含三行。
第一行包括一个整数n(1≤n≤3000)。
第二行包含两个浮点数a[0]和a[n+1]。
第三行包括n个浮点数,即c[1],c[2],...,c[n](保证-100≤c[i]≤100,-3000≤a[i]≤3000)。</span>

output

输出包含一行,即数字a[1](保留两位小数)。

sample_input

1
50.50 25.50
10.15

sample_output

27.85

hint


AC code:

1
#include<iostream>
#include<cstdio>
using namespace std;
struct D {
    double k,b;
    D (double x=0,double y=0) { k = x; b = y; }
};
D jian(D a,D b) {
    return D(a.k-b.k,a.b-b.b);
}
D X2 (D b) {
    return D(b.k*2,b.b*2);
}
D operator + (D a,double b) {
    return D(a.k,a.b+b);
}
D A[3005];
double C[3005];
int main(){
    int n;
    double x,y;
    while(scanf("%d",&n)!=EOF){
        scanf("%lf%lf",&x,&y);
        A[0] = D(0,x);
        A[1] = D(1,0);
        for(int i=1;i<=n;i++) scanf("%lf",&C[i]);
        for(int i=2;i<=n+1;i++) {
            A[i] = jian(X2(A[i-1]+C[i-1]) , A[i-2]);
        }
        double k,b;
        k = A[n+1].k;
        b = A[n+1].b;
        printf("%.2f\n",(y-b)/k);
    }
}

2
#include <iostream>
#include <cstdio>
using namespace std;

struct node
{
    double k;
    double b;
}nt[3010];

int main()
{
    int n;
    double First,Last,C;

    while(scanf("%d",&n)!=EOF)
    {
        scanf("%lf%lf",&First,&Last);
        nt[0].k=0,nt[0].b=First;
        nt[1].k=1.0,nt[1].b=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf",&C);
            nt[i+1].b=2*nt[i].b+2*C-nt[i-1].b;
            nt[i+1].k=2*nt[i].k-nt[i-1].k;
        }
        printf("%.2lf\n",(Last-nt[n+1].b)/nt[n+1].k);
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值