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 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
#include <stdio.h>
#include <stdlib.h>
int main(){
long long n, count;
double res=, tmp;
scanf("%lld", &n);
for (int i = ; i<n; i++){
count = (n - i)*(i + );
scanf("%lf", &tmp);
res += tmp*count;
}
printf("%.2lf", res); system("pause");
}
注意点:找到规律以后还要注意用long long,题目n不超过10e5,两个10e5乘起来就超过int范围了
PAT A1104 Sum of Number Segments (20 分)——数学规律,long long的更多相关文章
- 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 分)
题意:输入一个正整数N(<=1e5),接着输入N个小于等于1.0的正数,输出N个数中所有序列的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...
- PAT 甲级 1104. Sum of Number Segments (20) 【数学】
题目链接 https://www.patest.cn/contests/pat-a-practise/1104 思路 最容易想到的一个思路就是 遍历一下所有组合 加一遍 但 时间复杂度 太大 会超时 ...
- [PAT] 1144 The Missing Number(20 分)
1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...
- PAT甲级——A1104 Sum of Number Segments【20】
Consider a positive integer N written in standard notation with k+1 digits ai as ak⋯a1a0 ...
- PAT甲级——A1104 Sum of Number Segments
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...
- PAT (Advanced Level) 1104. Sum of Number Segments (20)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1104. Sum of Number Segments (20)-(水题)
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- A1104. Sum of Number Segments
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...
随机推荐
- 机器学习实战 - python3 学习笔记(一) - k近邻算法
一. 使用k近邻算法改进约会网站的配对效果 k-近邻算法的一般流程: 收集数据:可以使用爬虫进行数据的收集,也可以使用第三方提供的免费或收费的数据.一般来讲,数据放在txt文本文件中,按照一定的格式进 ...
- git与vscode连接的一种简单方式
首先你得安装git,如果你还没安装git,推荐你一个视频git的下载与安装,这套视频包含VS code和git的基本使用,当你看完它,就可以不看我的这篇博客了. 废话不多说,直接进入正题: 首先进入g ...
- a标签禁止跳转或者不跳转的几种实现方式
1.onclick事件中返回false <a href="http://www.baidu.com" onclick="return false" &g ...
- Javascript异步编程之三Promise: 像堆积木一样组织你的异步流程
这篇有点长,不过干货挺多,既分析promise的原理,也包含一些最佳实践,亮点在最后:) 还记得上一节讲回调函数的时候,第一件事就提到了异步函数不能用return返回值,其原因就是在return语句执 ...
- 排错-Loadrunner添加Windows Resource计数器提示“找不到网络路径”解决方法
Loadrunner添加Windows Resource计数器提示“找不到网络路径”解决方法 by:授客 QQ:1033553122 1.启动windows相关服务 win->services. ...
- (网页)angularjs中的interval定时执行功能(转)
转载博客园魔豆: 一个例子,用来显示当前实时时间,1秒钟刷新一次: <!DOCTYPE html> <html ng-app="myApp"> <he ...
- Django 知识总结(一)
Django已经学过的知识点: 1. Urls.py 路由系统: 正则 分组匹配 --> 位置参数 分组命名匹配 --> 关键字参数 分级路由 include 给路由起别名 name=&q ...
- plsql备份表---只是表---不包含表数据
写这个的同时还在备份,表的数据进度很慢,数据太大了. 用的工具是plsql 导出表:点击 tool工具 ---> export user object 导出用户目标 ----> ...
- sysfs_create_group创建sysfs接口
在调试驱动,可能需要对驱动里的某些变量进行读写,或函数调用.可通过sysfs接口创建驱动对应的属性,使得可以在用户空间通过sysfs接口的show和store函数与硬件交互: Syss接口可通过sys ...
- 用Python实现数据结构之链表
链表 链表与栈,队列不一样,它是由一个个节点构成的,每个节点存储着本身的一些信息,也存储着其他一个或多个节点的引用,可以从一个节点找到其他的节点,节点与节点之间就像是有链连在一起一样,这种数据结构就叫 ...