1104 Sum of Number Segments(20 分)
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) and (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 1. 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
#include<cstdio>
int main(){
int n;
scanf("%d",&n);
double v,ans = ;
for(int i = ; i <= n; i++){
scanf("%lf",&v);
ans += v * i * (n + - i);
}
printf("%.2f",ans);
return ;
}
最新文章
- Reading C type declarations(引用http://unixwiz.net/techtips/reading-cdecl.html)
- 变通实现微服务的per request以提高IO效率
- Linux 系统中僵尸进程
- 如何自定义jupyter notebook的主题
- 深入理解HTML表格
- 商业银行在CNAPS体系中对各种交易的处理
- java-二维码编写zxing
- hdu-5904 LCIS(水题)
- jdbc 得到表结构、主键
- Sublime text插件使用技巧
- hdu 2028
- Java theory and practice
- springmvc的3中路径风格
- Daily Scrum 12.7
- 在linux环境下配置node:node + npm + forever
- 在MAC中安装Compass的方法 (转)
- 一份传世典文:十年编程(Teach Yourself Programming in Ten Years)
- nginx、php-fpm默认配置与性能–TCP socket还是unix domain socket【转】
- 通过钩子程序跨程序关闭Window
- CMDB资产管理系统开发【day26】:admin action