PAT自主训练记录 甲级A1104/乙级B1049 数列的片段和

A1104 Sum of Number Segments(20分)


附有详细解释以及代码的博文链接:https://blog.csdn.net/qq_21394327/article/details/106975016
点击跳转


题目描述

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 10 ​ 5 10​^5 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

Thanks to Ruihan Zheng for correcting the test data.

题目大意

给一个序列,求出其所有真子序列元素的总和。

思路
这种题必然是有规律可循的,多写几个例子找规律。

注意
这题的测试数据应该更准确了,按照《算法笔记》配套习题书上的代码一直过不了,搜到的多数代码也一样过不了,都是测试点2答案错误。
找到一篇详细解释的文,点击看原文

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页