1104. Sum of Number Segments (20)【数学题】——PAT (Advanced Level) Practise

题目信息

1104. Sum of Number Segments (20)

时间限制200 ms

内存限制65536 kB

代码长度限制16000 B

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

解题思路

直接看代码吧,,不想多说,

AC代码

#include <cstdio>

int main()
{
    int n;
    long long st = 0;
    double s = 0, v;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i){
        scanf("%lf", &v);
        st += n - i - i;
        s += v * st;
    }
    printf("%.2f\n", s);
    return 0;
}
时间: 2024-10-30 22:53:31

1104. Sum of Number Segments (20)【数学题】——PAT (Advanced Level) Practise的相关文章

PAT 甲级 1104 Sum of Number Segments (20分)(有坑,int *int 可能会溢出)

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

PAT (Advanced Level) 1104. Sum of Number Segments (20)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n; double a[100000+10]; double b[100000+10]

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

PAT甲级——1104 Sum of Number Segments (数学规律、自动转型)

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 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,

1104 Sum of Number Segments(二刷)

英文题目:1104 Sum of Number Segments 中文题目:1049 数列的片段和 1 #include<iostream> 2 using namespace std; 3 4 int main() { 5 int n; 6 double t,sum = 0; 7 cin>>n; 8 for(int i = 0 ; i < n; ++i) { 9 cin>>t; 10 sum += (i+1)*t*(n-i); 11 } 12 printf(&q

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c

1104 Sum of Number Segments

题意: 给出n个不大于1.0的小数序列,如{ 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) and (0.4).现要求计算每个分片之和,即0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0. 思路:数学题

1081. Rational Sum (20)【模拟】——PAT (Advanced Level) Practise

题目信息 1081. Rational Sum (20) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B Given N rational numbers in the form "numerator/denominator", you are supposed to calculate their sum. Input Specification: Each input file contains one test case. Each case star

1088. Rational Arithmetic (20)——PAT (Advanced Level) Practise

题目信息 1088. Rational Arithmetic (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input fi