PAT甲级——A1029 Median
Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is 15. The median of two sequences is defined to be the median of the nondecreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.
Given two increasing sequences of integers, you are asked to find their median.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed that all the integers are in the range of long int.
Output Specification:
For each test case you should output the median of the two given sequences in a line.
Sample Input:
4 11 12 13 14
5 9 10 15 16 17
Sample Output:
13这道题,我要说道说道;
这道题和考究内存的使用,如果你的代码会去保存输入的两组数据,我相信,测试会提示“超内存”的错误;
所以,就保存一组数据,与另一组数据进行比较就行,不用保存;
一般简单的int数组远比任何容器都省内存!!!
#include <iostream>
using namespace std;
int main()
{
int res[], m, n, i, j, a, index = ;
cin >> m;
for (i = ; i <= m; ++i)
cin >> res[i];
cin >> n;
for (i = ,j=; i <= n; ++i)
{
cin >> a;
while (j <= m && a > res[j])
{
++index;
if (index == (m + n + ) / )
{
cout << res[j] << endl;
return ;
}
++j;
}
++index;
if (index == (m + n + ) / )
{
cout << a << endl;
return ;
}
}
while(j <= m)
{
++index;
if (index == (m + n + ) / )
{
cout << res[j] << endl;
return ;
}
++j;
}
return ;
}
PAT甲级——A1029 Median的更多相关文章
- PAT 甲级 1029 Median
https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968 Given an increasing se ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级1057. Stack
PAT甲级1057. Stack 题意: 堆栈是最基础的数据结构之一,它基于"先进先出"(LIFO)的原理.基本操作包括Push(将元素插入顶部位置)和Pop(删除顶部元素).现在 ...
- PAT甲级题分类汇编——理论
本文为PAT甲级分类汇编系列文章. 理论这一类,是让我觉得特别尴尬的题,纯粹是为了考数据结构而考数据结构.看那Author一栏清一色的某老师,就知道教数据结构的老师的思路就是和别人不一样. 题号 标题 ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- PAT甲级1119. Pre- and Post-order Traversals
PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...
随机推荐
- vue 绑定多个class
<div :class="[box,shadow]"></div> 或 :后面跟条件,条件成立则添加class,否则不添加 :class="[{' ...
- day 72 Django基础七之Ajax
Django基础七之Ajax 本节目录 一 Ajax简介 二 Ajax使用 三 Ajax请求设置csrf_token 四 关于json 五 补充一个SweetAlert插件(了解) 六 同源策略与 ...
- 第五篇:zTree节点的一些操作,权当备份
项目场景:将zTree的一个节点挪到某个已知的根节点下,因为树上的节点都是数据库查询出来的,所以不能直接用addNodes()这个方法(否则一刷新又恢复原样了),而是要把这个节点及其某些属性数据保存到 ...
- IDEA 创建普通的maven+java Project
最近想把以前积累的零散java练习和学习的东西建一个项目整理出来上传到码云托管,免得电脑挂了啥也找不到 配置是IDEA2017+java8+maven3.2.5,截图记录下步骤 第一步:File--& ...
- 20.multi_case05
#coding:utf-8 # 通过gather方法 import asyncio async def a(t): print('-->', t) await asyncio.sleep(0.5 ...
- springboot-配置多数据源(AOP实现)(HikariCP + MybatisPlus + mysql + SqlServer)
场景: springboot项目,默认使用HikariCP连接池 + MybatisPlus持久层框架 + mysql数据库等一套流程,现需求需去第三方sqlserver数据库拉取数据,直连数据库,不 ...
- C++ 系列:Boost Thread 编程指南
转载自:http://www.cppblog.com/shaker/archive/2011/11/30/33583.html 作者: dozbC++ Boost Thread 编程指南0 前言1 创 ...
- css3 ---2 属性的选择器
存在和值属性选择器1:[attr]:该选择器选择包含 attr 属性的所有元素,不论 attr 的值为何. [name]{ background: pink; } <!DOCTYPE html& ...
- 关于underline
1, 有些系统可能看不到下划线的效果,这是不妨按着Alt键看看: 2, underline=#, 数字表示在哪个字母下显示下划线,从0开始
- HTML - 表格标签相关
<html> <head></head> <body> <!-- table (表格) border : 表格的边框 width : 表格的宽 h ...