题意:

给出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. 0x5C 计数类DP

    cf 559C 考虑到黑色的格子很少,那么我把(1,1)变成黑色,然后按每个黑色格子接近终点的程度排序,计算黑色格子不经过另一个黑色格子到达终点的方案,对于当前的格子,要减去在它右下角的所有方案数(注 ...

  2. Spark 总结2

    网页访问时候 没有打开 注意防火墙! 启动park shell bin下面的spark-shell   这样启动的是单机版的 可以看到没有接入集群中: 应该这么玩儿  用park协议  spark:/ ...

  3. C#加密解密DES字符串<转>

    using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptograph ...

  4. numpy array或matrix的交换两行

    A[j,:] = A[maxindex,:] # 注意这样是一个很低级的错误!这样只是赋值 我们很容易想起python中的两个值交换一句搞定不用引入中间变量 a, b = b, a 但在numpy的a ...

  5. numpy常用函数之randn

    numpy中有一些常用的用来产生随机数的函数,randn就是其中一个,randn函数位于numpy.random中,函数原型如下: numpy.random.randn(d0, d1, ..., dn ...

  6. poj 2395 bfs/记录路径

    http://poj.org/problem?id=2935 Basic Wall Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  7. 计算机网络【七】:可靠传输的实现 (tcp窗口滑动以及拥塞控制)【转】

    转自:http://blog.chinaunix.net/uid-26275986-id-4109679.html TCP协议作为一个可靠的面向流的传输协议,其可靠性和流量控制由滑动窗口协议保证,而拥 ...

  8. 使用NSUserDefaults保存自定义对象(转)

    转自http://zani.iteye.com/blog/1431239 .h文件 #import <Foundation/Foundation.h> @interface MyObjec ...

  9. poj3268 Silver Cow Party (SPFA求最短路)

    其实还是从一个x点出发到所有点的最短路问题.来和回只需分别处理一下逆图和原图,两次SPFA就行了. #include<iostream> #include<cstdio> #i ...

  10. 掌握sudo的使用

    “sudo”是Unix/Linux平台上的一个非常有用的工具,它允许系统管理员分配给普通用户一些合理的“权利”,让他们执行一些只有超级用户或其他 特许用户才能完成的任务,比如:运行一些像mount,h ...