【测试点分析】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.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. 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.4Sample Output:
5.00
解题思路
最先想到的解法一定是二重循环累加。
但一般来讲, 200ms最多支持200w次左右的循环运算, 以本题最大数据量10w次计算, 10w*10w的数据量要远远高于200w,因此常规解法行不通。
对于这种大数据量的题, 很常见的一种解法是找规律, 找到规律,推导出数学公式, 就可以在O(n)的数量级内解题。
经过推导我们发现: 对于样例输入,有:
sum=0.1∗1∗4+0.2∗2∗3+0.3∗3∗2+0.4∗4∗1sum = 0.1*1*4 + 0.2*2*3 + 0.3*3*2 + 0.4*4*1sum=0.1∗1∗4+0.2∗2∗3+0.3∗3∗2+0.4∗4∗1
规律显而易见, 列for循环求解即可。
提交代码后, 发现样例2无法通过
百般调试无果后, 到网上寻找帮助。
大神对此的解答是:浮点型精度往往是有误差的,如1.2用浮点型数据保存,可能会变成:1.200003423213。
因此, 如果对浮点型精度进行大数据量的运算, 可能会导致误差累积,影响最终结果。
解决办法是: 将输入的小数乘1000,用long long型变量存储, 最后除以1000输出即可。
注意:网上的很多代码并非满分代码,测试点2无法通过!一定要注意甄别!
#include<bits/stdc++.h>
using namespace std;
using gg = long long;
int main() {gg n; scanf("%lld", &n);gg sum = 0;double x;for(gg i = 1; i <= n; i++) {scanf("%lf", &x);gg x1 = (gg)(x*10000+5)/10;sum += x1*(i*(n-i+1));}printf("%.2lf", (double)(sum/1000.0)); return 0;
}
——一生中总会遇到这样的时候,你的内心已经兵荒马乱天翻地覆了,可是在别人看来你只是比平时沉默了一点,没人会觉得奇怪。这种战争,注定单枪匹马。
【测试点分析】1104 Sum of Number Segments (20 分)相关推荐
- 1104. Sum of Number Segments (20)[数学逻辑题]
1. 原题:https://www.patest.cn/contests/pat-a-practise/1104 2. 思路: 题意:数学逻辑题.求所有连续子序列元素之和. 思路: 这题要找规律,即找 ...
- PAT甲级1104 Sum of Number Segments:[C++题解]数段之和,测试点2爆double,请用long double!!!
文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:本题题意比较简单,但是不能仅仅根据定义来算. 本题思路:统计每个数在多少个区间出现过,也就是这个数需要加多少次.共有n个数,其中第i个 ...
- 1104 Sum of Number Segments
1104 Sum of Number Segments 解题代码 测试结果 问题整理 解题代码 #include<iostream> #include<cstdio> usin ...
- 【PAT甲级 - C++题解】1104 Sum of Number Segments
✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
- PAT1104 Sum of Number Segments
PAT1104 原题 题目大意及思路 代码 运行截图 收获 原题 题目大意及思路 题中给定了一个正数序列,但这里我有两个疑惑点: 只说是0-1的正数,应该是个小数 序列本身是按照大小排列吗? 由于不认 ...
- 1144 The Missing Number (20 分)
Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...
- 1032 挖掘机技术哪家强 (20 分)(测试点分析)
为了用事实说明挖掘机技术到底哪家强,PAT 组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第 1 行给出不超过 10 5 的正整数 N,即参赛人数.随后 N ...
- PAT 1089 狼人杀-简单版(20 分)(代码+测试点分析)
1089 狼人杀-简单版(20 分) 以下文字摘自<灵机一动·好玩的数学>:"狼人杀"游戏分为狼人.好人两大阵营.在一局"狼人杀"游戏中,1 号玩家 ...
- 1081 Rational Sum (20 分)_22行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given N rational numbers in the form numerator/denominator, you a ...
最新文章
- 数据库连接池为什么要用threadlocal呢?不用会怎样?
- Navicat工具、pymysql模块
- python中的捕获异常、异常跟踪
- C#异步批量下载文件
- LeetCode之Palindrome Number(回文数)
- 软件测试qa等级考核制度,QA质量规范
- Java中String类中compareTo( )方法
- 【jmx】java jmx 获取 kafka topic的logStart LogEnd信息
- Java自动化测试框架-04 - TestNG之Test Method篇 - (详细教程)
- Django------多表操作
- 字符串ababaaab的nextval数组值序列为_子序列解题模板:最长回文子序列
- NeHe OpenGL教程 第四十四课:3D光晕
- 【渝粤教育】国家开放大学2018年春季 0550-22T素描(一) 参考试题
- Oracle 数据库锁机制
- java: 找不到符号 报错
- Typora设置工作台水印和导出PDF水印
- ASP.NET MVC入门视频教程
- SAP 系统图片修改以及 ABAP屏幕图片(大小调整)自适应设置
- 在路上●我的年轻●勇往直前●匆匆十年
- shell脚本实现批量解析域名IP地址(支持多DNS)