PAT甲级1029 Median

1029

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 nondecreasing 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.

Input Specification:
Each input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (2×10 
5
 ) 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.

Output Specification:
For each test case you should output the median of the two given sequences in a line.

Sample Input:
4 11 12 13 14
5 9 10 15 16 17
结尾无空行
Sample Output:
13
结尾无空行

折磨点:
1、当两个数组其中一个读取完毕时仍未找到中位数,那么需要继续遍历又或是通过公式直接输出剩余数组中存在的中位数

提供用例:
转载于用例

3 6 7 8

5 9 10 11 12 13

输出:9

5 8 9 10 11 12

3 4 5 6

输出:8

4 15 35 77 93

7 21 27 49 62 86 90 92

输出:62

自己写的代码太臃肿了,转载柳大神的代码:
柳大神

#include <iostream>
const int maxn = 200005;
int n, m, a1[maxn], a2[maxn], cnt = 0, i, j, ans;
int main() {
	scanf("%d", &n);
	for(i = 1; i <= n; i++)	scanf("%d", &a1[i]);
	scanf("%d", &m);
	for(i = 1; i <= m; i++)	scanf("%d", &a2[i]);
	//等同于判断数组总大小奇偶的问题,不用分开判断
	//等同于(第一个数组大小+第二个数组大小+1)/2
	int target = (n + m + 1) / 2;
	i = 1, j = 1;
	while(i <= n && j <= m) {
		ans = a1[i] <= a2[j] ? a1[i++] : a2[j++];
		if(++cnt == target)	break;
	}
	if(i <= n && cnt < target)	
		//这里target-cnt表示仍需要跳过该数目的数字,
		//因为需要使用下标,而此时下标所指向的数字本身就占
		//用了1,所以需要减1
		ans = a1[i + target - cnt - 1];
	else if(j <= m && cnt < target)	
		ans = a2[j + target - cnt - 1];
	printf("%d", ans);
	return 0;
}

本人提交版:

#include <iostream>
#include <vector>
using namespace std;
int main(){
    int a_n;
    cin >> a_n;
    vector<int> a(a_n);
    for(int i = 0;i < a_n;++i){
        cin >> a[i];
    }
    
    int b_n;
    cin >> b_n;
    int res = (a_n + b_n) / 2;
    if((a_n + b_n) & 1 == 1){
        res++;
    }
    if(a_n == 0){
        for(int i = 0;i < b_n;++i){
            int temp;
            cin >> temp;
            if(i == res - 1){
                cout << temp << endl;
                break;
            }
        }
    }
    else if(b_n == 0){
        cout << a[res - 1] << endl;
    }
    else{
        int count = 1;
        int i = 0;
        int flag = 0;
        for(int j = 0;j < b_n;++j){
            int temp;
            cin >> temp;
            while(temp >= a[i]&&i < a_n){
                if(count == res){
                    cout << a[i] << endl;
                    flag = 1;
                    break;
                }
                ++i;
                ++count;
            }
            if(flag == 1){
                break;
            }
            else{
                if(temp < a[i]||i == a_n){
                    if(count == res){
                        cout << temp << endl;
                        flag = 1;
                        break;
                    }
                    ++count;
                }
            }
        }
        if(flag == 0){
            for(;i < a_n;++i){
                if(count == res){
                    cout << a[i] << endl;
                    break;
                }
                ++count;
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值