PAT Sum of Number Segments[数学问题][一般]
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 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:
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[数学问题][一般]的更多相关文章
- PAT甲级——1104 Sum of Number Segments (数学规律、自动转型)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 1104 Sum of Number Segmen ...
- PAT 甲级 1104 sum of Number Segments
1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Pen ...
- PAT_A1104#Sum of Number Segments
Source: PAT A1104 Sum of Number Segments (20 分) Description: Given a sequence of positive numbers, a ...
- PAT1107:Sum of Number Segments
1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CAO, Pen ...
- 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 ...
- 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 ...
- PAT 甲级 1104. Sum of Number Segments (20) 【数学】
题目链接 https://www.patest.cn/contests/pat-a-practise/1104 思路 最容易想到的一个思路就是 遍历一下所有组合 加一遍 但 时间复杂度 太大 会超时 ...
- PAT 1104 Sum of Number Segments
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...
- PAT甲级——A1104 Sum of Number Segments【20】
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
随机推荐
- mysqldump备份时,--master-data选项的作用是什么?
需求描述: 今天在研究mysql的备份和恢复,使用mysqldump备份数据库时,用到--master-data选项, 在此,测试并记录选项的作用 测试过程: 1.不使用--master-data进行 ...
- [java] java 设计模式(2):抽象工厂模式(Abstract Factory)
工厂方法模式有一个问题就是,类的创建依赖工厂类,也就是说,如果想要拓展程序,必须对工厂类进行修改,这违背了闭包原则,所以,从设计角度考虑,有一定的问题,如何解决?就用到抽象工厂模式,创建多个工厂类,这 ...
- day19<异常&File类>
异常(异常的概述和分类) 异常(JVM默认是如何处理异常的) 异常(try...catch的方式处理异常1) 异常(try...catch的方式处理异常2) 异常(编译期异常和运行期异常的区别) 异常 ...
- vue 组件库
iView https://www.iviewui.com/ Radon UI https://luojilab.github.io/radon-ui/#!/ Element http://eleme ...
- JAVA Comparator 接口排序用法
java的比较器有两类,分别是Comparable接口和Comparator接口. 在为对象数组进行排序时,比较器的作用非常明显,首先来讲解Comparable接口. 让需要进行排序的对象实现Comp ...
- django实现瀑布流、组合搜索、阶梯评论、验证码
django实现图片瀑布流布局 我们在一些图片网站上经常会看到,满屏都是图片,而且图片都大小不一,却可以按空间排列.默认一个div是占用一行,当想把div里的图片并排显示的时候,只能使用float属性 ...
- 【Select2】带搜索框的下拉框美化插件
1 引入js css 文件 <script src="js/jquery-1.11.1.min.js"></script> <script src= ...
- java基础---->多线程之interrupt(九)
这里我们通过实例来学习一下java多线程中关于interrupt方法的一些知识.执者失之.我想当一个诗人的时候,我就失去了诗,我想当一个人的时候,我就失去了我自己.在你什么也不想要的时候,一切如期而来 ...
- stringstream读入每行数据
做了下阿里的编程测试题,就30分钟,不是正常的输入输入,直接给一个数组作为输入. 于是带想题和处理数据花了20分钟,最后10分钟搞一个dij模版, 竟然只过了66%,应该是我数组开小了. 题目数据量没 ...
- MongoDB复制集原理
版权声明:本文由孔德雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/136 来源:腾云阁 https://www.qclo ...