A1104. Sum of Number Segments
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence {0.1, 0.2, 0.3, 0.4}, we have 10 segments: (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0.4).
Now given a sequence, you are supposed to find the sum of all the numbers in all the segments. For the previous example, the sum of all the 10 segments is 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N, the size of the sequence which is no more than 105. The next line contains N positive numbers in the sequence, each no more than 1.0, separated by a space.
Output Specification:
For each test case, print in one line the sum of all the numbers in all the segments, accurate up to 2 decimal places.
Sample Input:
4
0.1 0.2 0.3 0.4
Sample Output:
5.00
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int N;
double ans = , temp;
scanf("%d", &N);
for(int i = ; i <= N; i++){
scanf("%lf", &temp);
ans += 1.0 * i * (N + - i) * temp;
}
printf("%.2lf", ans);
cin >> N;
return ;
}
总结:
1、主要是找规律, 计算出每个数一共出现几次(与位置有关),然后做乘法再累加即可。第i个数(从1开始)出现的次数是:i * (n + 1 - i)。
2、i * (N + 1 - i)有可能超过int的范围。 所以做类型转换时,1.0应该乘在式子的最前面。否则当int溢出后再做转换就无效了。
A1104. Sum of Number Segments的更多相关文章
- PAT A1104 Sum of Number Segments (20 分)——数学规律,long long
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...
- PAT甲级——A1104 Sum of Number Segments【20】
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- PAT甲级——A1104 Sum of Number Segments
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...
- PAT Advanced A1104 Sum of Number Segments (20) [数学问题]
题目 Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For e ...
- PAT_A1104#Sum of Number Segments
Source: PAT A1104 Sum of Number Segments (20 分) Description: Given a sequence of positive numbers, a ...
- PAT1107:Sum of Number Segments
1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Pen ...
- PAT Sum of Number Segments[数学问题][一般]
1104 Sum of Number Segments(20 分) Given a sequence of positive numbers, a segment is defined to be a ...
- PAT 甲级 1104 sum of Number Segments
1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Pen ...
- PAT甲级——1104 Sum of Number Segments (数学规律、自动转型)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 1104 Sum of Number Segmen ...
随机推荐
- Docker容器学习梳理 - 容器时间跟宿主机时间同步
在Docker容器创建好之后,可能会发现容器时间跟宿主机时间不一致,这就需要同步它们的时间,让容器时间跟宿主机时间保持一致.如下: 宿主机时间 [root@slave-1 ~]# date Fri M ...
- python-lambda用法
前言: lambda函数也叫匿名函数,即,函数没有具体的名称. 一.基础 lambda语句构建的其实是一个函数对象.匿名函数有个限制,就是只能有一个表达式,不用写return,返回值就是该表达式的结果 ...
- 12.18 Daily Scrum
最近大家确实都很忙,所以所有功能的实现要等到下周. Today's Task Tomorrow's Task 丁辛 实现和菜谱相关的餐厅列表. 实现和菜谱相关的餐厅列表. ...
- Linux内核设计期中总结
Linux内核设计期中总结 ● 知识点 一.计算机是如何工作的 计算机是按照冯·诺依曼存储程序的原理. 在执行程序时须先将要执行的相关程序和数据放入内存储器中,在执行程序时CPU根据当前程序指针寄存器 ...
- 【目标跟踪】相关滤波算法之MOSSE
简要 2010年David S. Bolme等人在CVPR上发表了<Visual Object Tracking using Adaptive Correlation Filters>一文 ...
- HDOJ2017_字符串统计
这是一道水题 HDOJ2017_字符串统计 #include<iostream> #include<string> #include<stdio.h> #inclu ...
- ThinkCMF项目部署出现无法加载数据库驱动解决方案
最近有个TP项目刚从从本地部署到阿里云服务器上,出现了无法加载数据库驱动的错误,提示 :( 无法加载数据库驱动: Think\Db\Driver 这里分享一下出现该错误的解决步骤: 首先记得项目部署到 ...
- 服务器端发送邮件签名采用Data URI scheme包含图片
要在服务器端基于HTML,拼接邮件内容,原来用户使用outlook采用了邮件签名,签名里含有公司Logo的图片,Outlook的msg文件里是专有的cid:xxxx,这里借用Data URI sche ...
- VC6到VC2010,项目迁移错误
错误信息: error C2440: “static_cast”: cannot from “BOOL (__thiscall CSelectRect::* )(void)” to “BOOL (__ ...
- Oracle18c OnlyExadataVersion 安装说明
1.根据惜分飞还有盖国强老师云和恩墨的文章, 验证了下OnlyExadataVersion的Oracle18c的数据库安装过程. 2.oracle参数修改以及创建用户, 目录, 修改.bash_pro ...