-
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 non-decreasing 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. -
Each input file may contain more than 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. -
For each test case you should output the median of the two given sequences in a line.
-
4 11 12 13 14 5 9 10 15 16 17
-
13
这道题要注意,对于一些占用较大的内存的声明,最好声明到函数体的外面,可能平时不太注意,如注释的部分。
-
题目描述:
-
输入:
-
输出:
-
样例输入:
-
样例输出:
#include<stdio.h>
void init(long int a, long int *s)
{
int i;
for(i = 0; i < a; i++)
{
scanf("%ld", &s[i]);
}
}
void findmin(long int a, long int b, long int s1[], long int s2[], long int *shunxu)
{
int min = 0, j = 0, i = 0;
for(; i < a; i++)
{
for(; j < b; j++)
{
if(s1[i] >= s2[j])
{
shunxu[min] = s2[j];
min++;
}
else if(s1[i] < s2[j])
{
shunxu[min] = s1[i];
min++;
break;
}
}
}
while(j < b)
{
shunxu[min] = s2[j];
j++;
min++;
}
while(i < a)
{
shunxu[min] = s1[i];
i++;
min++;
}
}
long int a, b, c;
long int s1[1000000],s2[1000000],shunxu[2000000];
int main()
{
//long int a, b, c;
//long int s1[1000000],s2[1000000],shunxu[2000000];
while(scanf("%ld", &a) != EOF)
{
init(a, s1);
scanf("%ld", &b);
init(b, s2);
findmin(a, b, s1, s2, shunxu);
c = a + b;
if(c % 2 == 0)
{
c = c / 2 - 1;
}
else
{
c = c / 2;
}
printf("%d\n", shunxu[c]);
}
return 0;
}
/**************************************************************
Problem: 1004
User: 不谙的风
Language: C++
Result: Accepted
Time:0 ms
Memory:32272 kb
****************************************************************/
****************************************************************/