简单算法练习-题目1004:Median

链接:http://ac.jobdu.com/problem.php?pid=1004

题目描述:

    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

趁着不忙,找点以前做过的题练练。好多算法都忘完了!九度这个平台不错,可用看自己以前的代码!

这个以前是用java做的,现在练练C。

#include <stdio.h>

#define MAX 1000000
int arr1[MAX];
int arr2[MAX];
int newArr[MAX<<1];
int main(){
	int len1,len2;
	while(scanf("%d", &len1) != EOF){
		int i;
		for(i=0; i<len1; i++)
			scanf("%d",&arr1[i]);
		
		scanf("%d", &len2);
		
		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;
}

16532 kb 10 ms

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]);
        }
 
    }
 
}

18368 kb 200 ms

发现C和java的内存占用相差不大,有必要再优化下,让数组也动态生成.

优化后: 内存和时间   908 kb 10 ms

#include <stdio.h>
#include <stdlib.h>
/*#define MAX 1000000*/
// int arr1[MAX];
// int arr2[MAX];
// int newArr[MAX<<1];
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;
}




你可能感兴趣的:(简单算法练习-题目1004:Median)