1.题目描述

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.
给定N个整数的递增序列S,中值是中间位置的数。例如,S1={11,12,13,14}的中值为12,S2={9,10,15,16,17}的中值为15。例如,S1和S2的中位数是13。给定两个整数的递增序列,要求找到它们的中值。

2.输入描述:

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 (<=1000000) 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.
每个输入文件包含一个测试用例。每个案例占2行,每一行提供一个序列的信息。对于每个序列,第一个正整数N(<=1000000)是该序列的大小。然后N个整数跟着,用一个空格隔开。保证所有整数都在长int的范围内。

3.输出描述:

For each test case you should output the median of the two given sequences in a line.
对于每个测试用例,您应该在一行中输出两个给定序列的中值。

4.输入例子:

4 11 12 13 14
5 9 10 15 16 17

5.输出例子:

13

6.源代码:

#include<stdio.h>
#include<stdlib.h>
#define MAX 1000000int main()
{   int *num1,*num2,*sort;num1=(int *)malloc(MAX * sizeof(int));num2=(int *)malloc(MAX * sizeof(int));sort=(int *)malloc(2 * MAX * sizeof(int));int i,j,k,M,N;//输入scanf("%d", &M);for(i = 0; i < M; i++)scanf("%d", &num1[i]);scanf("%d", &N);for(i = 0; i < N; i++)scanf("%d", &num2[i]);i=0,j=0,k=0;int mid=(M+N)/2;//中值位置//归并两个数组,排成一个有序的数组(参照归并排序)while(i < M && j < N && k <= mid){if(num1[i] < num2[j])sort[k++]=num1[i++];elsesort[k++]=num2[j++];}while(i < M && k <= mid)sort[k++]=num1[i++];while(j < N && k <= mid)sort[k++]=num2[j++];//输出if( (M+N)%2 == 0)printf("%d", sort[mid-1]);elseprintf("%d", sort[mid]);return 0;
}

Median(PAT)相关推荐

  1. PAT甲级1029 Median:[C++题解]贪心、二路归并

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 如果直接排序,时间复杂度是O(nlogn),n=2∗105O(nlogn),\ n=2*10^5O(nlogn), n=2∗105会 ...

  2. PAT 1029 Median

    题解 题意就是给你两个排好序的增序序列,求两个序列合并后的中位数. Input Specification: Each input file contains one test case. Each ...

  3. 【最新合集】PAT甲级最优题解(题解+解析+代码)

    以下每道题均是笔者多方对比后, 思考整理得到的最优代码,欢迎交流! 共同成长哇.可以和博主比拼一下谁刷的更快~ 欢迎收藏.欢迎来玩儿 PAT题解目录 题号 标题 题解 分类 使用算法 1001 A+B ...

  4. 【19行代码AC,简洁】1029 Median (25 分)

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given an increasing sequence S of N integers, the median is the n ...

  5. PAT甲级训练合集(1-70)

    本章题解跳转 考点 P1001 数字的数组表示和处理 P1002 多项式的数组表示和处理 P1003 深度优先搜素 P1004 深度优先搜素 P1005 哈希表 P1006 P1007 数组子区间求和 ...

  6. PAT 甲级-算法初步

    阅读原文 接上一篇 PAT 甲级-入门模拟 ,自我感觉这部分才是真正的算法入门,对基础的数据结构提供了很好的类型题进行匹配练习 包括分类:排序.散列.贪心.二分.双指针.打表.递推 排序 思想解释 排 ...

  7. 【PAT甲级】A1001-A1050刷题记录

    文章目录 A1001 A+B Format (20 分) 0.25 ★(一元多项式加法) A1002 A+B for Polynomials (25 分) 0.21 (单源最短路Dijkstra+边权 ...

  8. 【论文复现】中值滤波改进:Different Applied Median Filter(DAMF)

    Different Applied Median Filter(DAMF) DAMF解决的问题:中值滤波使用固定大小的模板.随着噪声密度的增加,固定大小的模板在消除椒盐噪声方面表现一般:​对于高密度椒 ...

  9. 刷PAT甲级的各题思路、细节以及遇到的问题记录

    1001 A+B Format (20分) 因为一定会用到字符串,而string非常好用,但是用的时候一定要注意不能越界访问,否则会在运行时出现abort() has been called. 100 ...

最新文章

  1. VarGFaceNet:地平线提出轻量级、有效可变组卷积的人脸识别网络
  2. python回归建模_Python实现回归预测及模型优化
  3. Office 2010 中的 UI 扩展性
  4. 恢复“资源管理器”右键弹出菜单中的“新建”“文本文件”。
  5. 将 instance 连接到 vlan100- 每天5分钟玩转 OpenStack(95)
  6. java中对象的序列化和反序列化
  7. Gem5在全系统(FS)模式下运行自己的测试程序
  8. 五大领域总目标指南_幼儿教师这样读《指南》事半功倍
  9. 绿洲因涉嫌抄袭下架;Facebook 泄露 4.19 亿条用户数据;Go 1.13 发布 | 极客头条...
  10. 为什么爬虫都用python_python为什么叫网络爬虫
  11. mac:文件编码问题
  12. springboot高校教室排课系统的设计与实现毕业设计-附源码221556
  13. 联想笔记本驱动升级,指纹识别不了不成功解决方法
  14. 计算机无法登陆提示rpc服务器不可用,电脑提示RPC服务器不可用的解决方法
  15. 贝叶斯信念网络简介以及算法整理笔记
  16. 商标变更的重要性不及时变更的损失
  17. 停车场停车怎么找车?怎样查车停车什么停车场?
  18. SMTP 与 ESMTP
  19. 华中科技大学有计算机科学与技术学院导师,华中科技大学计算机科学与技术学院导师简介-袁平鹏...
  20. 方舟服务器 mod文件夹,方舟mod文件夹应该放在哪 | 手游网游页游攻略大全

热门文章

  1. 【JZOJ4826】小澳的葫芦
  2. 汇佳学校凝心聚力缔造教育的第三选择
  3. 【JavaEE】HTTP(Part1 含面试题)
  4. RTSP流媒体协议视频平台EasyNVR和EasyNTS智能云组网同一浏览器运行为什么会导致EasyNTS无法登陆?
  5. android系统架构及源码目录结构
  6. python文案处理
  7. 丁成云师姐的-MySQL分享
  8. 【C语言实现】玩家互战五子棋(具体步骤和代码)
  9. 留言“说出你对中国乒乓的祝福”,60字左右
  10. Docker更改镜像仓库地址