题目

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

题目分析

给定⼀个正数数列,从中截取任意连续的⼏个数,称为⽚段。例如,给定数列{0.1, 0.2, 0.3, 0.4},可截取有(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个⽚段。给定正整数数列,求出全部⽚段包含的所有的数之和。如本例中10个⽚段总和是0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0,在⼀⾏中输出该序列所有⽚段包含的数之和,精确到⼩数点后2位

解题思路

  1. 找到每个数字在所有序列中出现次数的规律:如果当前是第i个数,则总出现次数等于i*(n+1-i)
  2. 计算总和时,只需遍历i,总和+=当前数字i(n+1-i)

易错点

  1. doubleintint和intintdouble,

    t+=i*(n+1-i)*m;//int*int*double 本题中n取值最大为10^5,所以int*int之后还是int可能越界,测试点2,3错误

    t+=m*(n+1-i)*i;//double*int*int 本题中n取值最大为10^5,但是double*int之后,结果隐式转换为double继续乘int,不会越界

Code

Code 01

#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
double sum = 0.0, temp;
for (int i = 1; i <= n; i++) {
cin >> temp;
sum = sum + temp * i * (n - i + 1);
}
printf("%.2f", sum);
return 0;
}

Code 01

#include <iostream>
using namespace std;
int main(int argc,char * argv[]) {
long long n;
scanf("%d",&n);
double m, t=0.0;
for(long long i=1; i<=n; i++) {
scanf("%lf",&m);
t+=i*(n+1-i)*m; //如果i定义为int这样写,测试点2,3不通过,因为n最大取值为10^5,int*int越界
//t+=m*i*(n+1-i);
}
printf("%.2f",t);
return 0;
}

PAT Advanced A1104 Sum of Number Segments (20) [数学问题]的更多相关文章

  1. PAT甲级——A1104 Sum of Number Segments【20】

    Consider a positive integer N written in standard notation with k+1 digits a​i​​ as a​k​​⋯a​1​​a​0​​ ...

  2. PAT甲级——A1104 Sum of Number Segments

    Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...

  3. PAT 甲级 1104. Sum of Number Segments (20) 【数学】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1104 思路 最容易想到的一个思路就是 遍历一下所有组合 加一遍 但 时间复杂度 太大 会超时 ...

  4. PAT 甲级 1104 sum of Number Segments

    1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Pen ...

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

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 1104 Sum of Number Segmen ...

  6. 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 ...

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

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  8. PAT甲题题解-1104. Sum of Number Segments (20)-(水题)

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  9. 【PAT甲级】1104 Sum of Number Segments (20 分)

    题意:输入一个正整数N(<=1e5),接着输入N个小于等于1.0的正数,输出N个数中所有序列的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...

随机推荐

  1. 083-PHP的foreach循环

    <?php $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $value = $value * 2; } print_r($a ...

  2. segger rtt 输出 log

    调试 mcu 的时候,使用 jlink 的 rtt 可以方便的输出信息. 输出有两种模式, client 和 logger. client 必须依附其他程序,比如说 mdk 调试状态,或者 logge ...

  3. [题解] LuoguP4609 [FJOI2016]建筑师

    传送门 首先对于高度为\(n\)的建筑,他的左边有\(A-1\)个建筑能被看到,右边有\(B-1\)个建筑能被看到,这两者类似,所以先来看左边. 一个建筑将会遮挡住它后面的高度比它矮的建筑,直到一个高 ...

  4. Python MySQL Where

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  5. 面向对象-static关键字实战案例

    面向对象-static关键字实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.static关键字概述 1>.static的功能 static关键字用于修饰成员变量和 ...

  6. iOS消息转发

    消息转发是一种功能强大的技术,可以大大增加Objective-C的表现力.什么是消息转发?简而言之,它允许未知的消息被困住并作出反应.换句话说,无论何时发送未知消息,它​​都会以一个很好的包发送到您的 ...

  7. python阴阳鱼绘制(使用turtle)

    from turtle import * def draw(radius ,color1 , color2): #设置画笔的大小 width(3) #设置画笔颜色和填充颜色 color("b ...

  8. hibernate 插入date值到postgresql,丢失时分秒

    用hibernate插入java.util.Date数据时发现 时分秒 会丢失.如 2014-05-30 15:59:16.921 在postgresql数据库中显示2014-05-30 00:00: ...

  9. UVALive 4731 dp+贪心

    这个题首先要利用题目的特性,先贪心,否则无法进行DP 因为求期望的话,越后面的乘的越大,所以为了得到最小值,应该把概率值降序排序,把大的数跟小的系数相乘 然后这种dp的特性就是转移的时候,由 i推到i ...

  10. 2020/2/6 PHP编程学习

    今天把后台数据库处理好了,用了框架后真就是搬砖的一天..晚上继续刷题,明天把数据库处理完,这样一个商城框架就有了: