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 10​5​​. 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

题目大意:给出一个序列,找出所有的子序列,并对子序列求和输出。

//首先就想到用树状数组,所以写了一个树状数组巩固一下,其中遇到了一些小问题,解决之后提交pat发现运行超时,只通过了两个测试点,应该是不能用这个来做了。

#include <iostream>
#include <stdio.h>
using namespace std;
float a[];
int lowbit(int x){
return x&-x;
} void update(int index,float x){//为什么你会死循环呢?
//因为!!树状数组下标应该从1开始的!!!
for(int i=index;i<=;i+=lowbit(i)){
a[i]+=x;
}
} float getsum(int x){
float ret=0.0;
for(int i=x;i>;i-=lowbit(i)){
ret+=a[i];
}
return ret;
} int main() {
int n;
scanf("%d",&n);
float x;
for(int i=;i<=n;i++){
scanf("%f",&x);
update(i,x);
}
// for(int i=1;i<=4;i++)
// printf("%f ",a[i]);
float sum=0.0;
for(int i=;i<=n;i++){
for(int j=i;j<=n;j++){
if(i==){
sum+=getsum(j);
}else {
sum+=(getsum(j)-getsum(i-));
} }
}
printf("%.2f",sum);
return ;
}

//基本上是O(n^2)的复杂度。

小问题:1.getsum函数没有对ret进行返回。。。导致sum一直输出nan....

2.树状数组下标应该从1开始!不是0,0会导致update死循环。

代码来自:https://www.liuchuo.net/archives/1921

#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
double a[];
double sum = 0.0;
for (int i = ; i <= n; i++) {
cin >> a[i];
sum = sum + a[i] * i * (n - i + );//这里计算每个数出现的次数!。
//是一个找规律的问题。
}
printf("%.2f", sum);
return ;
}

//当我看到这个题的代码如此短小精悍,惊呆了。

//还是有点不明白,搜索了一下:

对于第i个数字(i=0~n-1),它每组出现的次数为n-i,出现在前i+1个组中

非常厉害!学习了!

PAT Sum of Number Segments[数学问题][一般]的更多相关文章

  1. PAT甲级——1104 Sum of Number Segments (数学规律、自动转型)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 1104 Sum of Number Segmen ...

  2. PAT 甲级 1104 sum of Number Segments

    1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Pen ...

  3. PAT_A1104#Sum of Number Segments

    Source: PAT A1104 Sum of Number Segments (20 分) Description: Given a sequence of positive numbers, a ...

  4. PAT1107:Sum of Number Segments

    1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Pen ...

  5. PAT A1104 Sum of Number Segments (20 分)——数学规律,long long

    Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...

  6. PAT Advanced A1104 Sum of Number Segments (20) [数学问题]

    题目 Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For e ...

  7. PAT 甲级 1104. Sum of Number Segments (20) 【数学】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1104 思路 最容易想到的一个思路就是 遍历一下所有组合 加一遍 但 时间复杂度 太大 会超时 ...

  8. PAT 1104 Sum of Number Segments

    Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...

  9. PAT甲级——A1104 Sum of Number Segments【20】

    Consider a positive integer N written in standard notation with k+1 digits a​i​​ as a​k​​⋯a​1​​a​0​​ ...

随机推荐

  1. kafka原理

    今天因为工作接触kafka,先说说kafka是干嘛的. kafka: 说简单点他就是一个基于分布式的消息发布-订阅系统. 然后再理解一些专有名词: Kafka 专用术语 Broker:Kafka 集群 ...

  2. ScrollView拉到尽头时出现阴影的解决方法

    <code class="hljs markdown has-numbering" style="display: block; padding: 0px; col ...

  3. linux系统输入法设置

    首先是要安装了中文输入法,下面以搜狗为例. 2 从system settings 进入language support ,在keyboard input method system 中是看不到自己安装 ...

  4. 【微信开发】PC端 微信扫码支付成功之后自动跳转

    场景: PC端   微信扫码支付 结果: 支付成功 自动跳转 实现思路: 支付二维码页面,写ajax请求支付状态,请求到结果,无论成功还是失败,都跳转到相应的结果页面 具体实现方法: html部分: ...

  5. Android Framewrork资源类型有哪些?

    1. Google Framework res frameworks/base/core/res/res/values <public type="attr" name=&q ...

  6. java.lang.NoSuchMethodException:com.yxq.action.AdminAction.addGoods()《转载》

    java.lang.NoSuchMethodException:com.yxq.action.AdminAction.addGoods()   在学习struts2的时有时会出现此异常,现将其总结如下 ...

  7. 《转载》struts旅程《1》

    struts简介 Struts是Apache软件基金会(ASF)赞助的一个开源项目.它最初是jakarta项目中的一个子项目,并在2004年3月成为ASF的顶级项目.它通过采用JavaServlet/ ...

  8. rpm方式安装 gitlab centos7

    一.使用RPM安装 - 推荐 官方推荐的下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/ Tips 1 : Centos 7使用el7 ...

  9. 【BZOJ4698】Sdoi2008 Sandy的卡片 后缀数组+RMQ

    [BZOJ4698]Sdoi2008 Sandy的卡片 Description Sandy和Sue的热衷于收集干脆面中的卡片.然而,Sue收集卡片是因为卡片上漂亮的人物形象,而Sandy则是为了积攒卡 ...

  10. dd命令测试硬盘IO

    1.用Linux自带的命令dd测试硬盘IO,如下------------------------------------写: [root@server102 ~]# time dd count= of ...