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 ...
随机推荐
- Supervisor安装与配置(非守护进程管理工具)
http://blog.csdn.net/xyang81/article/details/51555473
- linux命令之find和locate
1.find / -name log.xml 按照名字查找log.xml文件 2.locate log.xml 查找log.xml文件(效率高) 3.grep 'hive' word. ...
- YARN 中的应用程序提交
YARN 中的应用程序提交 本节讨论在应用程序提交到 YARN 集群时,ResourceManager.ApplicationMaster.NodeManagers 和容器如何相互交互.下图显示了一个 ...
- glob模块--查询一个文件名列表
''' 在python中,glob模块是用来查找匹配的文件的 在查找的条件中,需要用到Unix shell中的匹配规则: * : 匹配所所有 ? : 匹配一个字符 *.* : 匹配如:[hello.t ...
- Nginx遇上Access Denied提示怎么解决
这几天在摆弄linux下面的各种服务器,对nginx非常有兴趣. 于是把phpmyadmin传上去了,先是phpmyadmin配了半天,结果配好之后发现phpmyadmin一些logo.css.js文 ...
- js里面setInterval和setTimeout相同点和区别
相同点:两个方法都是先触发间隔时间,再触发回调函数 区别: 1.setInterval每隔指定的时间就执行一次表达式,若不停止会一直执行下去 而setTimeout在执行时,是在载入后延迟指定时间后, ...
- 开发人员必读openstack网络基础2:交换机、路由器、DHCP
我们在使用openstack的过程中,会遇到创建虚拟机路由器.交换机等,那么1.他们的作用到底是什么?2.DHCP为什么会产生,它的作用是什么? 个人总结:交换机:一般用在同一网段,工作在数据链路层, ...
- LeetCode——Integer to Roman
Description: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the r ...
- 关于用phonegap 3.0+ 打包后sencha touch按钮点击切换动画延迟接近一秒的以及界面闪烁的解决方案
android的webview对硬件加速的支持貌似很不理想,在开启硬件加速的情况下,css3这些需要调用硬件加速的样式会大幅拖慢html5的webapp,在htc的部分手机上还会因开启硬件加速而导致闪 ...
- PHP概率算法---砸金蛋示例
这是一个很经典的概率算法: function get_rand($proArr) { $result = ''; //概率数组的总概率精度 $proSum = array_sum($proArr); ...