前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT 甲级 1104 sum of Number Segments

PAT 甲级 1104 sum of Number Segments

作者头像
ShenduCC
发布2018-04-27 10:49:55
4200
发布2018-04-27 10:49:55
举报
文章被收录于专栏:算法修养算法修养

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:

代码语言:javascript
复制
4
0.1 0.2 0.3 0.4 

Sample Output:

代码语言:javascript
复制
5.00

直接计算每个数出现了几次

公式:i*(n-i+1)

代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-06-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1104. Sum of Number Segments (20)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档