题意:

给出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. 完全重装python和yum

    本文原链接 http://smilepad.blog.51cto.com/6094369/1333478 http://blog.etc168.com/?p=642 1.删除现有Python #roo ...

  2. 【bzoj3240 && 洛谷P1397】矩阵游戏[NOI2013](矩阵乘法+卡常)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3240 这道题其实有普通快速幂+费马小定理的解法……然而我太弱了,一开始只想到了矩阵乘法的 ...

  3. java基础(8)-集合类

    增强for循环 /* *增强for循环 * for(元素类型 变量:数据或Collection集合){ * 使用变量即可,该变量就是元素 * } * 优点:简化了数组和集合的遍历 * 缺点:增强for ...

  4. Exception in thread "main" java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for topic_test_1219-2: 30010 ms has passed since batch creatio

    代码如下 public static void producer1() throws ExecutionException, InterruptedException { Properties pro ...

  5. python 之CORS,VUE+rest_framework示例

    一.跨域     浏览器的同源策略                 ----对ajax请求进行阻拦                 ----对href属性读不阻拦        xhr=new XML ...

  6. 使用dpkg时,提示:dpkg:处理软件包XXX时出错

    今天kali上安装搜狗输入法时,显示依赖关系问题,无法安装,百度搜了一下,找到解决方法. 使用dpkg时,提示:dpkg:处理软件包XXX时出错: 依赖关系问题,仍未被配置 类似于: 时,使用如下命令 ...

  7. CentOS6.4x84挂载U盘

    root用户登录 1. 查看磁盘情况: fdisk -l 信息如下: [root@CentOS6 ~]# fdisk -l Disk /dev/sda: 128.8 GB, 128849018880 ...

  8. MSDN 单机 MVC 帮助文档

    因为微软的mvc框架也是从开源框架演变而来的,所以微软没把mvc帮助文档放到单击帮助文档中.sososos下载好msdn单机帮助后,却找不到 System.Web.MVC 等命名空间的东西. 解决办法 ...

  9. swagger 在apache CXF 中的使用——JAX-RS Swagger2Feature

    The CXF Swagger2Feature allows you to generate Swagger 2.0 documents from JAX-RS service endpoints w ...

  10. Python学习之路day3-函数

    一.函数基础 编程方法典型的编程方法有面向过程.面向对象和函数式编程.面向过程是把编程的重点放在实现过程上,分析出结局问题所需的步骤过程,然后通过语句来一一定义实现.面向对象是把构成问题的事务分界成若 ...