poj 2601 Simple calculations
2024-08-28 08:41:23
Simple calculations
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6559 | Accepted: 3291 |
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.
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.
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
解题思路:
大概过程:
a[0]+a[2]-2a[1]-2c[1]=0
a[1]+a[3]-2a[2]-2c[2]=0
……
a[n-1] + a[n+1] - 2a[n] - 2c[n] = 0
累加可得:
a[0]+a[n+1]-a[1]-a[n]-2c[1]-2c[2]-...-2c[n]=0
依据a[n-1]+a[n+1]-2a[n]-2c[n]=0 => a[n+1]-2c[n]-a[n]=a[n]+2c[n]-a[n-1]
化简:a[0]+a[n]-a[1]-a[n-1]-2c[1]-2c[2]-...-2c[n-1]=0
同理:a[0]+a[n-1]-a[1]-a[n-2]-2c[1]-2c[2]-...-2c[n-2]=0
……
a[0]+a[2]-a[1]-a[1]-2c[1]=0
相加上面各式可得n*a[0]+a[n+1]-(n+1)*a[1]-2*n*c[1]-2*(n-1)*c[2]-...-2*c[n]=0
即a[1]=(n*a[0]+a[n+1]-2*n*c[1]-2*(n-1)*c[2]-...-2*c[n])/(n+1)
#include <iostream>
#include <iomanip>
using namespace std;
#define MAX 3005
int main(){
int n;
double a0,an;
double c[MAX];
while (cin>>n){
cin>>a0>>an;
double ans=0;
for (int i=0;i<n;i++){
cin>>c[i];
ans+=2*(n-i)*c[i];
}
ans=(n*a0+an-ans)/(n+1);
cout<<fixed<<setprecision(2)<<ans<<endl;
}
return 0;
}
最新文章
- AB窗体互传参数
- 新浪ip地址转换接口
- Condition的await-signal流程详解
- Android Capture Android System Audio
- 使用getJSON()方法异步加载JSON格式数据
- Count Primes 解答
- 在SSMS里查看TDS数据包内容
- VS2012 安装出错 :通道正在关闭
- http://www.cnblogs.com/ycxyyzw/archive/2012/07/31/2616951.html
- Asp.Net Core 轻松学-多线程之Task(补充)
- MySQL 笔记整理(11) --怎么给字符串字段加索引?
- H5测试与PC端测试不同的点
- jquery for循环判断是否重复
- cookie保存登录的用户名和密码
- freeswitch 获取当前网关通道数
- android to hide the keybord
- 课程五(Sequence Models),第一 周(Recurrent Neural Networks) —— 3.Programming assignments:Jazz improvisation with LSTM
- EF扩展 更新指定字段
- nodejs 通过nginx后出现响应慢的解决方法
- input元素默认选中设置
热门文章
- 【后缀数组】【线段树】poj3974 Palindrome
- python3开发进阶-Django框架的自带认证功能auth模块和User对象的基本操作
- 3.3常用类(java学习笔记)Runtime与Process
- MathType如何插入竖直线
- 转载:win10 下安装32位的Oracle 11g 客户端(问题:环境不满足最低要求)
- Linux下使用Python操作MySQL数据库
- wireshark----教你怎样抓包
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:3.安装Oracle RAC-3.2.安装 cvuqdisk 软件包
- UI 层级问题
- iOS小技巧 - 如何生成范围随机数