题意:

给出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. 初识Spring security-无Security的SpringMVC

    百度百科定义: Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了 ...

  2. 【bzoj2151】种树(堆/优先队列+双向链表)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2151 这道题因为优先队列不怎么会用,而且手写堆的代码也不长,也想复习一下手写堆的写法…… ...

  3. mybatis 中if标签判断boolean 的写法。

    mybatis 的if 比较标签在比较数值时可以这样写: <if test="value=0"> </if> 在比较字符串时可以这么写: <if te ...

  4. SpringCloud-高可用的分布式配置中心(config)

    当服务实例很多时,都从配置中心读取文件,这是可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用 新建一个注册中心 pom如下 <?xml version="1.0" ...

  5. QT QDockWidget锚接部件 和 QTreeWidget 树形部件 构成树形选择项

    1.  如图,在mainwindow中 添加DockWidget到右侧,里面镶嵌TreeWidget. 2. QTreeWidget *treeWidget = new QTreeWidget; // ...

  6. Nginx虚拟主机配置模板

    /////////////////////////////写在前头//////////////////////////////////////////Nginx 服务器中文文档:http://www. ...

  7. JavaScrip 原生多文件上传及预览 兼容多浏览器

    JavaScrip 原生多文件上传及预览 兼容多浏览器 html代码块 <div class="container"> <label>请选择一个图像文件:& ...

  8. 路由器分配的IP地址

    在IP地址范围内,一部分地址将保留作为私人IP地址空间,专门用于内部局域网使用,这些地址如下表: A类 10.0.0.0-10.255.255.255 网络数:1 B类 172.16.0.0-172. ...

  9. CentOS 6.6 中 mysql_5.6 主从数据库配置

    [mysql5.6 主从复制] 1.配置主从节点的服务配置文件 1.1.配置master节点 [mysqld] binlog-format=row log-bin=master-bin log-sla ...

  10. poj 2513 欧拉图/trie

    http://poj.org/problem?id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submi ...