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) (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.4

Sample Output:

5.00

注意点:第一次提交的时候sum = sum+i*(n-i+1)*a; 有两个测试点不能通过; 修改为sum = sum+1.0*(n-i+1)*i,或者sum=sum+a*i*(n-i+1)就是正确的,并且1.0必须前程才是正确的,。原因不是很明白,以后为了保险起见小数和整数相乘的时候,均把小数写在前
 1 #include<iostream>
 2 using namespace std;
 3 int main(){
 4   int n, i;
 5   double a, sum=0;
 6   cin>>n;
 7   for(i=1; i<=n; i++){
 8     cin>>a;
 9     sum = sum + a*i*(n-i+1);
10   }
11   printf("%.2f", sum);
12   return 0;
13 }

原文地址:https://www.cnblogs.com/mr-stn/p/9142618.html

时间: 2024-08-29 23:49:05

1104 Sum of Number Segments (20)的相关文章

1104. Sum of Number Segments (20)【数学题】——PAT (Advanced Level) Practise

题目信息 1104. Sum of Number Segments (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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)

PAT 甲级 1104 Sum of Number Segments (20分)(有坑,int *int 可能会溢出)

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

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

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n; double a[100000+10]; double b[100000+10]

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

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 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,

1104 Sum of Number Segments(二刷)

英文题目:1104 Sum of Number Segments 中文题目:1049 数列的片段和 1 #include<iostream> 2 using namespace std; 3 4 int main() { 5 int n; 6 double t,sum = 0; 7 cin>>n; 8 for(int i = 0 ; i < n; ++i) { 9 cin>>t; 10 sum += (i+1)*t*(n-i); 11 } 12 printf(&q

1104 Sum of Number Segments

题意: 给出n个不大于1.0的小数序列,如{ 0.1, 0.2, 0.3, 0.4 },则共有10个分片(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).现要求计算每个分片之和,即0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0. 思路:数学题

3 sum allow number used multi times

class Solution { public: vector<vector<int> > ThreeSum(int a[], int n, int target) { vector<vector<int> > ret; for (int i = 0; i < n; i++) { if(i>0 && a[i] == a[i - 1]) continue; int begin = i, end = n - 1; while (beg

A题目

1 1001 A+B Format(20) 2 1002 A+B for Polynomials(25) 3 1003 Emergency(25) 4 1004 Counting Leaves(30) 5 1005 Spell It Right(20) 6 1006 Sign In and Sign Out(25) 7 1007 Maximum Subsequence Sum(25) 8 1008 Elevator(20) 9 1009 Product of Polynomials(25) 10

PAT甲级目录

树 备注 1004 Counting Leaves   1020 Tree Traversals   1043 Is It a Binary Search Tree  判断BST,BST的性质 1053 Path of Equal Weight   1064 Complete Binary Search Tree  完全二叉树的顺序存储,BST的性质 1066 Root of AVL Tree  构建AVL树,模板题,需理解记忆 1079 Total Sales of Supply Chain