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 ...
随机推荐
- 058-PHP中goto语句的使用
<?php for($i=1;$i<=5;$i++){ //使用for循环循环输出1~5 if($i==3) goto ECH; //当$i为3时候跳出for循环 echo "$ ...
- 057-while循环
<?php $x=1; //初始化变量 while($x<=5){ //执行while循环 echo "$x<br />"; $x++; } ?>
- 如何为 .NET Core 安装本地化的 IntelliSense 文件
在.Net Core 2.x 版本,Microsoft 官方没有提供 .Net Core 正式版的多语言安装包.因此,我们在用.Net Core 2.x 版本作为框架目标编写代码时,智能提成是英文的. ...
- 浅谈Python 中 __getattr__与__getattribute__的区别
__getattr__与__getattribute__均是一般实例属性截取函数(generic instance attribute interception method),其中,__getatt ...
- hdu 2583 How far away ? 离线算法 带权求最近距离
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- cf442 B.Andrey and Problem
看题偷瞄到题解2333(以为是劲题呢..结果是乱贪心,奇怪) 排序之后,如果加入下一个比现在更优就更新答案(奇怪啊) t=ans*(1-a[i])+s*a[i];(ans*(1-a[i])是新的一位不 ...
- 在线上Linux下,PHP扩展安装(使用yum安装)
直接操作linux,在命令模式下用yum 来安装PHP的扩展: 扩展:mbstring 命令: yum install php-mbstring* 扩展:GD库 命令:yum install php- ...
- kettle 数据库连接失败
kettle 数据库连接失败 测试连接提示缺少驱动. 提示错误信息:Driver class 'oracle.jdbc.driver.OracleDriver' could not be found, ...
- ArchLinux安装(UEFI)
ArchLinux安装(UEFI) 一.连接网络 1.连接 # wifi-menu 2.检查是否联通 (ctrl+c停止) # ping www.baidu.com 3.远程安装 注:连接上网络之后可 ...
- Python爬虫之解析网页
常用的类库为lxml, BeautifulSoup, re(正则) 以获取豆瓣电影正在热映的电影名为例,url='https://movie.douban.com/cinema/nowplaying/ ...