题目1004:Median

本文介绍了一种算法,该算法用于找到两个已排序整数序列的中位数。通过将两个序列合并并排序,然后选取中间元素作为中位数。示例展示了如何输入两个序列并输出它们的中位数。

题目描述:

    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 145 9 10 15 16 17

样例输出:

13


#include <iostream>
#include <algorithm>

using namespace std;

bool cmp(const int &a, const int &b)
{
    if(a > b)
        return 0;
    if(a < b)
        return 1;
}
int main()
{
    int num1,num2;
    while(cin >> num1)
    {
        int *arr1 = new int[num1];
        for(int i = 0; i < num1; i++)
        {
            cin >> arr1[i];
        }

        cin >>num2;
        int *arr2 = new int[num2+num1];
        for(int i = 0; i < num1; i++)
        {
            arr2[i] = arr1[i];
        }
        for(int i = 0; i <num2; i++)
        {
            cin >> arr2[i + num1];
        }
        sort(arr2,arr2+(num1+num2),cmp);
        cout << arr2[(num1 + num2 + 1)/2-1] << endl;

    }
}



在 SQL 中,不同的数据库系统对于计算中位数的函数和方法有所不同。以下分别给出几种常见数据库(MySQL、SQL Server、Oracle)的实现方式。 ### MySQL 实现 在 MySQL 中,需要通过自定义逻辑来计算中位数。以下是示例代码: ```sql -- 假设表名为 emp,部门号字段为 deptid,工资字段为 salary SELECT deptid, -- 计算中位数 AVG(salary) AS median_salary FROM ( SELECT deptid, salary, -- 对每个部门的工资进行排序 @row_num := IF(@prev_dept = deptid, @row_num + 1, 1) AS row_num, -- 计算每个部门的总记录数 @total_rows := IF(@prev_dept = deptid, @total_rows, (SELECT COUNT(*) FROM emp WHERE deptid = e.deptid)), @prev_dept := deptid FROM emp e, (SELECT @row_num := 0, @total_rows := 0, @prev_dept := NULL) vars ORDER BY deptid, salary ) ranked -- 筛选出中位数所在的行 WHERE CASE WHEN @total_rows % 2 = 1 THEN row_num = (@total_rows + 1) / 2 WHEN @total_rows % 2 = 0 THEN row_num IN (@total_rows / 2, (@total_rows / 2) + 1) END GROUP BY deptid; ``` ### SQL Server 实现 在 SQL Server 中,可以使用 `PERCENTILE_CONT` 函数来计算中位数。示例代码如下: ```sql -- 假设表名为 emp,部门号字段为 deptid,工资字段为 salary SELECT deptid, -- 使用 PERCENTILE_CONT 函数计算中位数 PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) OVER (PARTITION BY deptid) AS median_salary FROM emp GROUP BY deptid; ``` ### Oracle 实现 在 Oracle 中,同样可以使用 `PERCENTILE_CONT` 函数来计算中位数。示例代码如下: ```sql -- 假设表名为 emp,部门号字段为 deptid,工资字段为 salary SELECT deptid, -- 使用 PERCENTILE_CONT 函数计算中位数 PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) OVER (PARTITION BY deptid) AS median_salary FROM emp GROUP BY deptid; ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值