1104. Sum of Number Segments
来源:互联网 发布:sql server 2008免费版 编辑:程序博客网 时间:2024/06/11 09:15
1104. Sum of Number Segments (20)
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:40.1 0.2 0.3 0.4Sample Output:
5.00
#include<iostream>#include<vector>using namespace std;int main(){ int n; double a,sum = 0; vector<double> res; cin>>n; for(int i = 0;i<n;++i){ cin>>a; res.push_back(a); } int size = res.size(); for(int i = 0;i<n;++i) sum += res[i]*(n-i)*(i+1); printf("%.2lf\n",sum); return 0;}
- 1104. Sum of Number Segments
- 1104. Sum of Number Segments
- 1104. Sum of Number Segments
- 1104. Sum of Number Segments
- 1104. Sum of Number Segments
- 【PAT】1104. Sum of Number Segments (20)
- 1104. Sum of Number Segments (20)
- 1104. Sum of Number Segments (20)
- 1104. Sum of Number Segments (20)
- PAT 1104. Sum of Number Segments (20)
- 1104. Sum of Number Segments (20)
- 1104. Sum of Number Segments (20)
- PAT 1104. Sum of Number Segments (20)
- 1104. Sum of Number Segments (20)
- [pat]1104. Sum of Number Segments (20)
- 1104. Sum of Number Segments (20)
- pat 1104. Sum of Number Segments
- 1104. Sum of Number Segments (20)
- define宏定义中的#,##,@#及\符号
- Stanford机器学习---第一讲. Introduction of machine learning
- MyEclipse设置统一编码格式
- 15、Java Web 路径问题
- OGNL简介
- 1104. Sum of Number Segments
- TCP/IP协议族各层的协议汇总
- python调用bat 整理
- C#OOP之八 继承 接口和抽象类
- 怎样更好的使用搜索
- 朱、刘算法:求最小树形图权值个人理解+个人详解【最小树形图模板】
- 自定义PopupWindow轻松实现从底部弹出
- spring boot 自定义logback日志配置文件
- 1105. Spiral Matrix