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 ...
随机推荐
- RabbitMQ 延时消息设计
问题背景 所谓"延时消息"是指当消息被发送以后,并不想让消费者立即拿到消息,而是等待指定时间后,消费者才拿到这个消息进行消费. 场景一:客户A在十二点下了一个订单,我想半个小时后来 ...
- git bash返回上一级目录
YITU-LIUMZ+Administrator@yitu-liumz MINGW64 ~/learngit/gitskills (dev)$ cd ..\ 注意 cd 后面有空格 然后就会弹出一个 ...
- 通过清华TUNA镜像源下载Android源码
建议参考官方教程:https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/ 以下为自己测试时执行的步骤: 1.下载repo $ curl https://mirr ...
- 系统、决策、控制研究系列(SSDC)
本类目主要介绍的书籍来自springer的系列书籍中的一本,对于该系列书籍介绍如下: “系统.决策及控制研究”(SSDC)系列涵盖了在广泛认知的系统.决策及控制的各个领域的快速.最新和高质量的最新发展 ...
- Docker-Compose 安装
1. 什么是Docker-Compose Compose项目来源于之前的fig项目,使用python语言编写,与docker/swarm配合度很高. Compose 是 Docker 容器进行编排的工 ...
- Windows 版本下 Oracle12.1.0.2 升级Oracle12.2.0.1的步骤
oracle12.1.0.1 2013年发布的产品 2014年左右发布12.1.0.2 2016年底发布了 oracle12.2.0.1 经常有人会安装了最早的oracle版本,然后需要升级到最新的o ...
- Angular 行内式依赖注入
var app = angular.module('myApp', ['ng']); //创建一个自定义服务app.factory('$Debug', function () { return { d ...
- scipy线性模块liner(linalg)
#liner import numpy as np from scipy import linalg as lg arr=np.array([[1,1],[0,1]]) matr=np.mat('[1 ...
- ES6 Set & Map
ES6 Set & Map OK ES6 Map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Globa ...
- Delphi下EasyGrid使用体会
最近在编写软件的时候,非常需要一款支持多表头的StringGrid控件,朋友介绍使用EasyGrid控件,这款控件大概从04年开始就没有再更新,网上有关与它的资料也较少.但是通过其demo,此软件还是 ...