题目

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.4) (0.3) (0.3, 0.4) (0.4).

Now given a sequence, you are supposed to find the sum of all the numbers in all the segments. For the previous example, the sum of all the 10 segments is 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 =

5.0.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N, the size of the sequence which is no more than 105. The next line contains N positive numbers in the sequence, each no more than 1.0, separated by a space.

Output Specification:

For each test case, print in one line the sum of all the numbers in all the segments, accurate up to 2 decimal places.

Sample Input:

4

0.1 0.2 0.3 0.4

Sample Output:

5.00

题目分析

给定⼀个正数数列,从中截取任意连续的⼏个数,称为⽚段。例如,给定数列{0.1, 0.2, 0.3, 0.4},可截取有(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) (0.4) 这10个⽚段。给定正整数数列,求出全部⽚段包含的所有的数之和。如本例中10个⽚段总和是0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0,在⼀⾏中输出该序列所有⽚段包含的数之和,精确到⼩数点后2位

解题思路

  1. 找到每个数字在所有序列中出现次数的规律:如果当前是第i个数,则总出现次数等于i*(n+1-i)
  2. 计算总和时,只需遍历i,总和+=当前数字i(n+1-i)

易错点

  1. doubleintint和intintdouble,

    t+=i*(n+1-i)*m;//int*int*double 本题中n取值最大为10^5,所以int*int之后还是int可能越界,测试点2,3错误

    t+=m*(n+1-i)*i;//double*int*int 本题中n取值最大为10^5,但是double*int之后,结果隐式转换为double继续乘int,不会越界

Code

Code 01

#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
double sum = 0.0, temp;
for (int i = 1; i <= n; i++) {
cin >> temp;
sum = sum + temp * i * (n - i + 1);
}
printf("%.2f", sum);
return 0;
}

Code 01

#include <iostream>
using namespace std;
int main(int argc,char * argv[]) {
long long n;
scanf("%d",&n);
double m, t=0.0;
for(long long i=1; i<=n; i++) {
scanf("%lf",&m);
t+=i*(n+1-i)*m; //如果i定义为int这样写,测试点2,3不通过,因为n最大取值为10^5,int*int越界
//t+=m*i*(n+1-i);
}
printf("%.2f",t);
return 0;
}

最新文章

  1. vim vi Ubuntu
  2. Web基础知识
  3. 【Junit】JUnit-4.12使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误
  4. 解决Download interrupted: Connection to https://dl-ssl.google.com refused的问题
  5. TextView过长显示省略号, TextView文字中间加横线
  6. [转]The culture name list in C#
  7. git-svn 的使用
  8. 在VC++中启用内存泄露检测
  9. HTML5学习知识点
  10. Spring之AOP模块
  11. SpringIOC原理简述
  12. Jmeter选项含义
  13. falsk 项目中日志设置
  14. .Net 常用插件及第三方库
  15. 01:CENTOS使用VIRTUALENV搭建独立的PYTHON环境-PYTHON虚拟环境
  16. PHP Cron Expression Parser ( LARAVEL )
  17. 移动网络应用开发中,使用 HTTP 协议比起使用 socket 实现基于 TCP 的自定义协议有哪些优势?
  18. MapReduce 应用实例
  19. input text 不显示输入的历史记录
  20. bzoj 3779 重组病毒 —— LCT+树状数组(区间修改+区间查询)

热门文章

  1. JDBC获取数据库连接慢
  2. 第一个flink application
  3. 深度解析Critical Thinking的四个阶段
  4. ACM-奇特的立方体
  5. [BJDCTF2020]The mystery of ip
  6. python_生成器小结
  7. POJ 1845:Sumdiv 快速幂+逆元
  8. java集合对象区别二
  9. 2020/2/3 PHP代码审计之PHP弱类型
  10. POJ 1017:Packets