Find Median
Time Limit: |
2000MS |
Memory Limit: |
65535K |
Description
一个长度为L(L≥1)的升序序列S,处在第⌈L/2⌉个位置的数称为S的中位数。例如,若序列S1=(11,13,15,17,19),则S1的中位数是15。两个序列的中位数是含它们所有元素的升序序列的中位数。例如,若S2=(2,4,6,8,20),则S1和S2的中位数是11。现有两个等长升序序列A和B,找出A和B的中位数。
Input
第一行:升序序列A(都是正整数);
第二行:升序序列B(都是正整数);
Output
输出A和B的中位数
Sample Input
11 13 15 17 19
2 4 6 8 20
Sample Output
11
Hint
No hint.
Source
解题代码:
大牛解法:
#include<stdio.h>
#include<algorithm>
using namespace std;
int n[1000000];
int main()
{
int c=0;
while(scanf("%d",&n[c])!=EOF)
c++;
sort(n,n+c);
printf("%d\n",n[c/2-1]);
}
我的代码:
#include<iostream>
using namespace std;
int n[1000000];
int m[1000000];
int main()
{
int c=0;
int a=0;
while(cin>>a)
{
n[c++]=a;
if(cin.get()=='\n')
break;
}
c=0;
while(cin>>a)
{
m[c++]=a;
if(cin.get()=='\n')
break;
}
int i=0,j=0,k=0;
for(;k<=c-1;k++)
if(n[i]<m[j])
i++;
else
j++;
c=n[i-1]>m[j-1]?n[i-1]:m[j-1];
cout<<c<<endl;
}
欢迎指教!!!