题意:

给出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;
    ,tmp;
    scanf("%d",&n);
    ;i<=n;i++){
        scanf("%lf",&tmp);
        //sum+=tmp*(i*(n-i+1));//错误  语句1
        sum+=tmp*i*(n-i+);//正确
    }
    printf("%.2f\n",sum);
    ;
}

1104 Sum of Number Segments的更多相关文章

  1. PAT 甲级 1104 sum of Number Segments

    1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Pen ...

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

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 1104 Sum of Number Segmen ...

  3. 1104 Sum of Number Segments(20 分)

    Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...

  4. PAT 甲级 1104. Sum of Number Segments (20) 【数学】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1104 思路 最容易想到的一个思路就是 遍历一下所有组合 加一遍 但 时间复杂度 太大 会超时 ...

  5. PAT 1104 Sum of Number Segments

    Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...

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

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  7. PAT甲题题解-1104. Sum of Number Segments (20)-(水题)

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  8. 【PAT甲级】1104 Sum of Number Segments (20 分)

    题意:输入一个正整数N(<=1e5),接着输入N个小于等于1.0的正数,输出N个数中所有序列的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...

  9. PAT1107:Sum of Number Segments

    1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Pen ...

随机推荐

  1. juniper ssg 常用命令

    netscreen juniper ssg操作命令   2013年4月10日   命令行下取得配置信息 get config   命令行下取得相应时间设置 get clock    set vrout ...

  2. js动态添加和删除标签

    html代码 <h1>动态添加和删除标签</h1> <div id="addTagTest"> <table> <thead& ...

  3. makefile下$(wildcard $^),$^,$@,$?,$<,$(@D),$(@F) 含义

    makefile下$(wildcard $^),$^,$@,$?,$<,$(@D),$(@F)代表的不同含义 $(filter-out $(PHONY) $(wildcard $^),$^) 常 ...

  4. STL视频_00

    [05:40]比赛规则 - Part01[06:33]比赛规则 - Part02[07:28]比赛规则 - Part03[08:45]提出的问题 - 1和2[09:23]提出的问题 - 3和4 *** ...

  5. js备忘录模式

    备忘录(Memento):在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状态. 备忘录模式比较适用于功能比较复杂的,但需要维护或记录属性 ...

  6. 一款简易的CSS3扁平化风格联系表单

    CSS3扁平化风格联系表单是一款CSS3简易联系表单非常清新,整体外观不是那么华丽,但是表单扁平化的风格让人看了非常舒服,同时利用了HTML5元素的特性,表单的验证功能变得也相当简单.经测试效果相当不 ...

  7. SSAS——基础--cube

    SSAS——基础   一.Analysis Services Analysis Services是用于决策支持和BI解决方案的数据引擎.它提供报表和客户端中使用的分析数据. 它可在多用途数据模型中创建 ...

  8. 【LABVIEW到C#】1》ini的操作

    using System; using System.IO; using System.Drawing; using System.Collections; using System.Componen ...

  9. WPF的Presenter(ContentPresenter)

    WPF的Presenter(ContentPresenter) 2010-12-20 14:34 by Clingingboy, 10619 阅读, 3 评论, 收藏, 编辑 这是2年前写了一篇文章 ...

  10. sql print

    这个因为你使用了varchar+int ,但是print只支持一种类型的输出,你要么通过转换函数将@no转换成字符类型,要么去掉@name.print '李勇' + convert(varchar, ...