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.

思路:数学题,找规律

1、以0.1  0.2  0.3  0.4  0.5 为例

0.1总共会被加1个5次(1*5),即由0.1作为起点发起的,0.1 、0.1  0.2、0.1  0.2  0.3、0.1  0.2  0.3  0.4、0.1  0.2  0.3  0.4  0.5

0.2总共会被加2个4次(2*4),即由0.1作为起点发起的,0.1  0.2、0.1  0.2  0.3、0.1  0.2  0.3  0.4、0.1  0.2  0.3  0.4  0.5

以及由0.2作为起点发起的,0.2、0.2  0.3、0.2  0.3  0.4、0.2  0.3  0.4  0.5

以此类推,某个数被相加的次数等于其左侧的个数(包括其自身)与其右侧的个数(包括其自身)之积,如下表


a[i]

0.1 

0.2

0.3

0.4

0.5

在a[i]左侧的个数(包括a[i]本身)

1

2

3

4

5

在a[i]右侧的个数(包括a[i]本身)

5

4

3

2

1

下标i

1

2

3

4

5

2、不注意细节会有两个测试点通不过!

因为n的最大值为100,000,因此语句1整数部分乘积最大为50,000*50,000>2^31-1,从而会造成溢出!(细节!基础!)

代码:

#include <stdio.h>
int main()
{
    int n;
    double sum=0,tmp;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%lf",&tmp);
        //sum+=tmp*(i*(n-i+1));//错误  语句1
        sum+=tmp*i*(n-i+1);//正确
    }
    printf("%.2f\n",sum);
    return 0;
}

原文地址:https://www.cnblogs.com/kkmjy/p/9533187.html

时间: 2024-11-06 03:36:35

1104 Sum of Number Segments的相关文章

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 (数学规律、自动转型)

本文同步发布在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,

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

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 (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

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]

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

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

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