两个排序数组求中值
来源:互联网 发布:mac ndk环境变量 编辑:程序博客网 时间:2024/05/18 20:49
- #include "iostream"
- #include "stdio.h"
- #include "math.h"
- #include "map"
- #include "vector"
- #include "queue"
- #include "memory.h"
- #include "algorithm"
- #include "string"
- using namespace std;
- #define N 1000005
- #define INF 1<<29
- #define max(a,b) a>b?a:b
- int a[N],b[N];
- int array3[N];
- int MergeArray(int la,int ha,int lb,int hb)
- {
- int i,j,x;
- i=la,j=lb,x=0;
- while(i<=ha&&j<=hb)
- {
- if(a[i]>b[j]) array3[x]=b[j++];
- else array3[x]=a[i++];
- x++;
- }
- while(i<=ha) array3[x++]=a[i++];
- while(j<=hb)array3[x++]=b[j++];
- x--;
- return array3[x/2];
- }
- int main()
- {
- int m,n,i,j;
- while(scanf("%d",&n)!=EOF)
- {
- for(i=0;i<n;i++)
- scanf("%d",&a[i]);
- cin>>m;
- for(j=0;j<m;j++)
- scanf("%d",&b[j]);
- int la,lb,ha,hb,ma,mb;
- la=lb=0;ha=n-1;hb=m-1;
- int res;
- res=MergeArray(la,ha,lb,hb);
- printf("%d\n",res);
- }
- }
解法2:(利用algorithm 中的nth_element())
- #include "iostream"
- #include "stdio.h"
- #include "math.h"
- #include "map"
- #include "vector"
- #include "queue"
- #include "memory.h"
- #include "algorithm"
- #include "string"
- using namespace std;
- #define N 1000005
- #define INF 1<<29
- #define max(a,b) a>b?a:b
- long a[1000000];
- int main()
- {
- long n1,n2,i,mid;
- while(cin>>n1)
- {
- for(i=0;i<n1;i++)
- cin>>a[i];
- cin>>n2;
- for(;i<n1+n2;i++)
- cin>>a[i];
- mid=(i-1)/2;
- nth_element(a,a+mid,a+i);
- cout<<a[mid]<<endl;
- }
- return 0;
- }
- 两个排序数组求中值
- 求两个有序数组的中值
- 算法20:两个排序数组merge后的中值
- 求两个排序数组的并集
- 两个排序的数组求交集
- leetcode 求两个排序数组的中位数
- 求两个排序数组的中位数
- 求两个排序数组的交集
- 求两个排序数组的交集
- 求两个排序数组的中间值
- 求两个排序数组的中位数
- 【2017.12.3】4.Median of Two Sorted Arrays(两个排序数组的中值)难
- LeetCode—寻找两个有序数组中值
- 求两个相同大小已排序数组中的中位数
- 【题目27】求两个排序数组的交集
- 求两个或多个已排序数组的中位数
- 求两个排序好的数组的重复项
- Arithmetic problem | 求两个排序数组的中位数
- Quartz 2D Programming Guide
- 图形图像处理网文
- IOS 四种保存数据的方式!
- 我喜欢的些东西收藏
- list操作
- 两个排序数组求中值
- 数据库分页
- Hash函数
- 视频监控存储空间大小与传输带宽计算方法
- JAVA double 类型相加
- .C# 操作SQLite数据库
- android如何获得手机屏幕大小
- 树状数组简单运用 hdu1166
- SQL 存储过程分页