首页 > 其他分享 >PAT_A1104 Sum of Number Segments

PAT_A1104 Sum of Number Segments

时间:2023-10-27 09:33:54浏览次数:42  
标签:PAT sequence 0.1 Segments 0.2 0.4 int Sum 0.3

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 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
#include<bits/stdc++.h>
using namespace std;
long double t, r=0;
int main(){
	int n; scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%Lf", &t);
		// r += i*(n+1-i)*t;
		r += t*i*(n+1-i);
	}
	printf("%.2Lf\n", r);
	return 0;
}

总结
1、 #数学问题/简单数学
2、测试点3报错,原因是起初写的r += i*(n+1-i)*t;,i是int类型,最大可达$10^5$ ,两个int相乘会溢出。改变一下相乘的顺序即可:r += t*i*(n+1-i);
3、测试点2报错,原因是double精度不够,这里改用long double类型。
链接:[[../../c++语法/long double|long double]]

标签:PAT,sequence,0.1,Segments,0.2,0.4,int,Sum,0.3
From: https://www.cnblogs.com/Afinis/p/17791044.html

相关文章

  • PAT 甲级【1012 The Best Rank】
    本题用java极容易超时,提交了好几次才成功另外9088777750,名次应该是12335,不是12334 importjava.io.*;publicclassMain{@SuppressWarnings("unchecked")publicstaticvoidmain(String[]args)throwsIOException{StreamTokenizer......
  • 003Square(n)Sum(8kyu)from codewars
    Square(n)SumCompletethesquaresumfunctionsothatitsquareseachnumberpassedintoitandthensumstheresultstogether.完成平方和函数,对每个传入其中的数字平方并相加。defsquare_sum(numbers):sums=0foriinnumbers:sums+=i*iretu......
  • PAT_B1003 我要通过!
    “答案正确”是自动判题系统给出的最令人欢喜的回复。本题属于PAT的“答案正确”大派送——只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”。得到“答案正确”的条件是:字符串中必须仅有 P、 A、 T这三种字符,不可以包含其它字符;任意形如 xPA......
  • gerrit 将他人改动直接 打patch到自己代码上
    原文:https://blog.csdn.net/qq_21438461/article/details/131362485 在Linux中,patch命令用于将补丁文件应用到源代码文件中,从而实现对源代码的修改。patch命令的详细描述如下:patch命令用于将补丁文件应用到源代码文件中,以实现对源代码的修改。补丁文件通常是由开发者或者社区......
  • E. Segment Sum
    E.SegmentSumYouaregiventwointegers$l$and$r$($l\ler$).Yourtaskistocalculatethesumofnumbersfrom$l$to$r$(including$l$and$r$)suchthateachnumbercontainsatmost$k$differentdigits,andprintthissummodulo$998244353$.Fo......
  • ExcelPatternTool 开箱即用的Excel工具包现已发布!
    目录ExcelPatternTool功能特点:快速开始使用说明常规类型高级类型Importable注解Exportable注解IImportOption导入选项IExportOption导出选项单元格样式StyleMapping样式映射使用数据库作为数据源示例Sample1:不同类型字段导出Sample2:高级类型导入和导出Sample3:员工健康体检工具已知......
  • Leecode 1. 两数之和 Two Sum
    给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。你可以按任意顺序返回答案。示例1:输入:nums=[2,7,11......
  • PAT_A1101 Quick Sort
    Thereisaclassicalprocessnamed partition inthefamousquicksortalgorithm.Inthisprocesswetypicallychooseoneelementasthepivot.Thentheelementslessthanthepivotaremovedtoitsleftandthoselargerthanthepivottoitsright.Given ......
  • Qt CustomDashLine会对范围外Path自动裁剪问题
    在使用QPainter进行绘制时发现问题。当直接使用QPen进行绘制自定义虚线时会出现一个问题:当绘制的Path遇到界面进行裁剪时,此时虚线线型将会省略裁剪的那一部分,导致自定义虚线在移动以及放大时会自动修改位置。解决办法:直接使用QPainterPathSkroke。问题描述......
  • PAT 甲级【1011 World Cup Betting】
    importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.StreamTokenizer;publicclassMain{@SuppressWarnings("unchecked")publicstaticvoidmain(String[]args)throwsIOException{StreamTokenizerin=ne......