【PAT甲级 - C++题解】1104 Sum of Number Segments

简介: 【PAT甲级 - C++题解】1104 Sum of Number Segments

1104 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) and (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


题意

给定一个正数序列,每个非空连续子序列都可被称作一个数段。


例如,给定序列 { 0.1, 0.2, 0.3, 0.4 },该序列共包含 10 个不同数段,(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) 。


现在给定一个序列,请你求出该序列的所有数段中所有数字的总和。


对于前面的示例,10 个数段的总和为 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0。

思路

我们可以假设当前要算的是第 i 个数:


那么在 0 ~ i 之间包含 i 的连续子字段有 i 个,而在 i ~ n 之间包含 i 的连续子字段有 n - i + 1 个。


所以只用将两部分拼起来就可以得到在 0 ~ n 之间包含 i 的连续子字段数量,即一共 i * (n - i + 1) 个。故我们可以从前往后依次计算每个数的总值,即得到 a[i] * i * (n - i + 1) 。


注意,答案输出保留 2 位小数,并且要用 long double 去存储数值,double 的精度不够。


代码

#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N];
int main()
{
    int n;
    cin >> n;
    long double res = 0;
    for (int i = 1; i <= n; i++)
    {
        long double x;
        cin >> x;
        res += x * i * (n - i + 1);
    }
    printf("%.2Lf\n", res);
    return 0;
}
目录
相关文章
|
11月前
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
35 0
|
11月前
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
55 0
|
11月前
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
49 0
|
11月前
|
存储 C++ 容器
【PAT甲级 - C++题解】1057 Stack
【PAT甲级 - C++题解】1057 Stack
48 0
|
11月前
|
存储 C++
【PAT甲级 - C++题解】1055 The World‘s Richest
【PAT甲级 - C++题解】1055 The World‘s Richest
58 0
|
6月前
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
64 1
|
6月前
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
21 0
LeetCode 136. 只出现一次的数字 Single Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode contest 177 5169. 日期之间隔几天 Number of Days Between Two Dates
LeetCode contest 177 5169. 日期之间隔几天 Number of Days Between Two Dates
|
算法
LeetCode 414. Third Maximum Number
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
66 0
LeetCode 414. Third Maximum Number