博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT 甲级 1104 sum of Number Segments
阅读量:4922 次
发布时间:2019-06-11

本文共 1486 字,大约阅读时间需要 4 分钟。

1104. Sum of Number Segments (20)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CAO, Peng

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:
40.1 0.2 0.3 0.4
Sample Output:
5.00
 
直接计算每个数出现了几次
公式:i*(n-i+1)
 
#include 
#include
#include
#include
#include
#include
using namespace std;int n;double x;int main(){ double ans; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%lf",&x); ans+=x*i*(n-i+1); } printf("%.2f\n",ans); return 0;}

转载于:https://www.cnblogs.com/dacc123/p/8228593.html

你可能感兴趣的文章
[hyper-V] centos 7 安装 java
查看>>
cmd(或者说DOS窗口)输出内容到文件
查看>>
matlab之sub2ind()函数
查看>>
弹出窗
查看>>
mysql 链接报 Can't connect to MySQL server on 'localhost' (10061)
查看>>
hdu 4288 Coder(单点操作,查询)
查看>>
HDU 4760 Good FireWall 完好Trie题解
查看>>
HDU 2037 今年暑假不AC (贪心)
查看>>
ARM架构和编程-4
查看>>
大北农路:翻转,花钱免灾
查看>>
问题:DataGrid该行并不总是很清楚验证错误(删除),解决方案,如下面
查看>>
JAVA修饰符类型(public,protected,private,friendly)
查看>>
C语言默认參数值的实现
查看>>
docker 技术
查看>>
javascript挑战编程技能-第二题:计算字符数
查看>>
osgEarth编译
查看>>
win10与linux双系统切换时间不一致的调整
查看>>
基于JS实现发送短信验证码后的倒计时功能(无视页面刷新,页面关闭不进行倒计时功能)...
查看>>
Enum Binding ItemsSource In WPF
查看>>
Javaweb 实现一个简单留言板
查看>>