首页 > ACM题库 > 九度OJ > 九度-1004-Median[解题代码]
2013
12-12

九度-1004-Median[解题代码]

题目来源:2011年浙江大学计算机及软件工程研究生机试真题

题目描述:

    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

cpp 代码如下:
#include <stdio.h>
#include <stdlib.h>
int * arr1,* arr2,* newArr;

int main(){
	int len1,len2;
	while(scanf("%d", &len1) != EOF){
		
		arr1 = (int *)malloc(len1*4);
		int i;
		for(i=0; i<len1; i++)
			scanf("%d",&arr1[i]);
		

		scanf("%d", &len2);
		
		arr2 = (int *)malloc(len2*4);
		newArr = (int *)malloc( (len1+len2)*4);
		for(i=0; i<len2; i++)
			scanf("%d",&arr2[i]);
		i=0;
		int index=0,j=0;
		while(i<len1 && j<len2){
			if(arr1[i] < arr2[j])
				newArr[index++] = arr1[i++];
			else
				newArr[index++] = arr2[j++];
		}

		while(i <len1)
			newArr[index++] = arr1[i++];
		while(j <len2)
			newArr[index++] = arr2[j++];

		printf("%d\n",newArr[(index-1)/2]);

	}

	return 0;
}
/**************************************************************
	Problem: 1004
	User: coder
	Language: C
	Result: Accepted
	Time:0 ms
	Memory:912 kb
****************************************************************/

java 代码如下:

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
	static int arr1[];
	static int arr2[];
	static int arr[];
	public static void main(String[] args) {
		Scanner s = new Scanner(new BufferedInputStream(System.in));
		while(s.hasNextInt()){
			int n = s.nextInt();
			arr1 = new int[n];
			for(int i=0; i<n; i++)
				arr1[i] = s.nextInt();
			int m = s.nextInt();
			arr2 = new int[m];
			arr = new int[m+n];
			for(int i=0; i<m; i++){
				arr2[i] = s.nextInt();
			}
			int i=0;
			int j=0;
			int k=0;
			while(i<n && j<m){
				if(arr1[i] <arr2[j]){
					arr[k++] = arr1[i++];
				}
				else{
					arr[k++] = arr2[j++];
				}
			}
			while(i<n)
				arr[k++] = arr1[i++];
			while(j<m)
				arr[k++] = arr2[j++];
			System.out.println(arr[(arr.length-1)/2]);
		}

	}

}

/**************************************************************
	Problem: 1004
	User: coder
	Language: Java
	Result: Accepted
	Time:200 ms
	Memory:18368 kb
****************************************************************/


  1. 5.1处,反了;“上一个操作符的优先级比操作符ch的优先级大,或栈是空的就入栈。”如代码所述,应为“上一个操作符的优先级比操作符ch的优先级小,或栈是空的就入栈。”