PAT Advanced A1104 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:
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位
解题思路
- 找到每个数字在所有序列中出现次数的规律:如果当前是第i个数,则总出现次数等于i*(n+1-i)
- 计算总和时,只需遍历i,总和+=当前数字i(n+1-i)
易错点
- 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) [数学问题]的更多相关文章
- PAT甲级——A1104 Sum of Number Segments【20】
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- PAT甲级——A1104 Sum of Number Segments
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...
- PAT 甲级 1104. Sum of Number Segments (20) 【数学】
题目链接 https://www.patest.cn/contests/pat-a-practise/1104 思路 最容易想到的一个思路就是 遍历一下所有组合 加一遍 但 时间复杂度 太大 会超时 ...
- PAT 甲级 1104 sum of Number Segments
1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Pen ...
- PAT甲级——1104 Sum of Number Segments (数学规律、自动转型)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 1104 Sum of Number Segmen ...
- 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 ...
- PAT (Advanced Level) 1104. Sum of Number Segments (20)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1104. Sum of Number Segments (20)-(水题)
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- 【PAT甲级】1104 Sum of Number Segments (20 分)
题意:输入一个正整数N(<=1e5),接着输入N个小于等于1.0的正数,输出N个数中所有序列的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...
随机推荐
- 125-PHP类__set()魔术方法
<?php class ren{ //定义一个人类 //定义成员属性 private $name='tom'; private $age=15; //定义__set()魔术方法 public f ...
- 5分钟搞懂:session与cookie
http是无状态协议 无状态协议的意思是服务端与客户端不会记录任何一次通信的信息.诺兰有一部电影<记忆碎片>,说的是一个有"短期记忆丧失症"的人根据自己支离破碎的记忆来 ...
- webpack散记--Typescript
Typescript 1.js的超集 官网:typescriptlang.org/tslang.cn 来自:微软 安装:官方的 npm i typescript ts-loader --save-d ...
- Django实现websocket
django实现websocket大致上有两种方式,一种channels,一种是dwebsocket.channels依赖于redis,twisted等 一 dwebsocket 1 Django实现 ...
- java 学生信息管理
题目: 一.测试要求: 1.按照测试内容要求完成程序的设计与编程: 2.将最终结果的源文件(.java)文件上传到以班级为单位,保存源程序. 3.建立学号姓名文件夹,如: ...
- java课程之团队开发冲刺阶段2.4
总结昨天进度: 1.照例学习了课前提醒的功能,不可否认的是,在这个功能上,需要的技术和之前的上课静音有点相似,都是通过广播然后开启service服务,然后进行每分钟的监听,查看时间是否一致,在一致的情 ...
- java web实现在线编辑word,并将word导出(一)
前段时间领导交代了一个需求:客户需要一个能够web在线编辑文字,如同编辑word文档一样,同时能够将编辑完成的内容导出为word文档并下载到本地. 我们选择了前台使用富文本插件的形式用于编辑内容,使用 ...
- Apache的网站,使用Nginx进行反向代理(1个IP绑定多个域名,对应多个网站)解决方案
同一个端口是不能同时有两个程序监听的.所以换个思路解决同一台服务器下某些网站运行在nginx下,某些网站运行在Apache下共存. 解决思路: 将nginx作为代理服务器和web服务器使用,nginx ...
- Linux学习-第二章(命令)20200216
- c 循环左移
char b[11] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; const int iShift = 4; for (int j = ...