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. python2.0_s12_day19_前端模版使用

    Django中引用bootstrap实现在前端可以创建客户信息,可以修改客户信息我们需要设计一个前端用户交互系统.我们在设计之前,讨论一些需求:前端实现:1. 不同角色的用户,看到的东西是不一样的 销 ...

  2. java 字符编码问题

    编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串.但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是 ...

  3. php学习六:字符串

    前言:越来越觉得php的强大之处了,不紧是数组,在字符串方面也可以看出它的优势,第一:方法多,集合了js,c,c#等多门语言的方法:第二:有许多方法是其他语言不具备的,如他的模糊比较,就是其他语言所没 ...

  4. 时序数据库技术体系 – InfluxDB 多维查询之倒排索引

    本文转自: http://hbasefly.com/2018/02/09/timeseries-database-5/ 在时序数据库概述一文中,笔者提到时序数据库的基础技术栈主要包括高吞吐写入实现.数 ...

  5. delphi 获取网卡信息(支持多网卡)

    delphi 获取网卡信息(支持多网卡) unit LGetAdapterInfo; interface uses Windows, SysUtils, Classes; const MAX_HOST ...

  6. java框架---->quartz整合spring(一)

    今天我们学习一下quartz的定时器的使用.年轻时我们放弃,以为那只是一段感情,后来才知道,那其实是一生. quartz的简单实例 测试的项目结构如下: 一.pom.xml中定义quartz的依赖 & ...

  7. HTTP/2笔记之帧

    零.前言 客户端和服务器端一旦握手协商成功接建立连接,端点之间可以基于HTTP/2协议传递交换帧数据了. 一.帧通用格式 下图为HTTP/2帧通用格式:帧头+负载的比特位通用结构: +-------- ...

  8. BOM history对象

    history对象的三个可用方法和一个属性 back();后退 forward();前进 go(n);跳到第几个页面,负数为后退,正数为前进 length属性,获取缓存的页面的数量 安全性考虑,his ...

  9. sencha touch 入门系列 (九) sencha touch 布局layout

    布局用来描述你应用程序中组件的大小和位置,在sencha touch中,为我们提供了下面几种布局: 1.HBox: HBox及horizontal box布局,我们这里将其称为水平布局,下面是一段演示 ...

  10. IPMI相关漏洞利用及WEB端默认口令登录漏洞

    IPMI相关漏洞 0套件漏洞 使用0套件时,只需要Username,口令任意即可绕过身份鉴别执行指令.而且一般还有一个默认的账户admin或者ADMIN. 备注:IPMI是一套主机远程管理系统,可以远 ...