6-11 求自定类型元素序列的中位数(浙大PTA-C语言编程基础题目集)

做题时只提交核心函数的代码即可

// 核心函数

ElementType Median(ElementType array[], int num);

ElementType FindKthLargestNumber(ElementType array[], int num, int k);

int Partition(ElementType array[], int num);

void Swap(ElementType *a, ElementType *b);

ElementType Median(ElementType array[], int num)
{
    ElementType median = FindKthLargestNumber(array, num, (num + 1) / 2);

    return median;
}

ElementType FindKthLargestNumber(ElementType array[], int num, int k)
{
    if (num < k)
    {
        perror("Invalid input");
        exit(1); // TODO: error handling
    }

    if (1 == num)
    {
        return array[0];
    }

    int pivotIndex = Partition(array, num);

    if (pivotIndex == k - 1)
    {
        return array[pivotIndex];
    }

    if (pivotIndex > k - 1)
    {
        return FindKthLargestNumber(array, pivotIndex, k);
    }

    return FindKthLargestNumber(array + (pivotIndex + 1),
                                num - (pivotIndex + 1),
                                k - (pivotIndex + 1));
}

int Partition(ElementType array[], int num)
{
    int pivotIndex = num - 1;
    ElementType pivot = array[pivotIndex];

    int left = 0;
    int right = num - 2;

    while (left <= right)
    {
        for (; left <= right && array[left] > pivot; ++left)
            ;
        for (; left <= right && array[right] <= pivot; --right)
            ;

        if (left < right)
        {
            Swap(&array[left], &array[right]);
            ++left;
            --right;
        }

        // if (left == right)  is impossible

        if (left > right)
        {
            Swap(&array[left], &array[pivotIndex]);
            pivotIndex = left;
            break;
        }
    }

    return pivotIndex;
}

void Swap(ElementType *a, ElementType *b)
{
    ElementType tmp = *a;
    *a = *b;
    *b = tmp;
}

本题要求实现一个函数,求N个集合元素A[]的中位数,即序列中第⌊(N+1)/2⌋大的元素。其中集合元素的类型为自定义的ElementType

函数接口定义:

ElementType Median(ElementType A[], int N);

其中给定集合元素存放在数组A[]中,正整数N是数组元素个数。该函数须返回NA[]元素的中位数,其值也必须是ElementType类型。

完整的可执行代码(可以直接在本地编译并运行):

#include <stdio.h>

#define MAX_NUMBER 10
typedef float ElementType;

ElementType Median(ElementType A[], int N);

int main(void)
{
    ElementType array[MAX_NUMBER];
    int N = 0;

    scanf("%d", &N);
    for (int idx = 0; idx < N; ++idx)
    {
        scanf("%f", &array[idx]);
    }
    printf("%.2f\n", Median(array, N));

    return 0;
}

ElementType Median(ElementType array[], int num);

ElementType FindKthLargestNumber(ElementType array[], int num, int k);

int Partition(ElementType array[], int num);

void Swap(ElementType *a, ElementType *b);

ElementType Median(ElementType array[], int num)
{
    ElementType median = FindKthLargestNumber(array, num, (num + 1) / 2);

    return median;
}

ElementType FindKthLargestNumber(ElementType array[], int num, int k)
{
    if (num < k)
    {
        perror("Invalid input");
        exit(1); // TODO: error handling
    }

    if (1 == num)
    {
        return array[0];
    }

    int pivotIndex = Partition(array, num);

    if (pivotIndex == k - 1)
    {
        return array[pivotIndex];
    }

    if (pivotIndex > k - 1)
    {
        return FindKthLargestNumber(array, pivotIndex, k);
    }

    return FindKthLargestNumber(array + (pivotIndex + 1),
                                num - (pivotIndex + 1),
                                k - (pivotIndex + 1));
}

int Partition(ElementType array[], int num)
{
    int pivotIndex = num - 1;
    ElementType pivot = array[pivotIndex];

    int left = 0;
    int right = num - 2;

    while (left <= right)
    {
        for (; left <= right && array[left] > pivot; ++left)
            ;
        for (; left <= right && array[right] <= pivot; --right)
            ;

        if (left < right)
        {
            Swap(&array[left], &array[right]);
            ++left;
            --right;
        }

        // if (left == right)  is impossible

        if (left > right)
        {
            Swap(&array[left], &array[pivotIndex]);
            pivotIndex = left;
            break;
        }
    }

    return pivotIndex;
}

void Swap(ElementType *a, ElementType *b)
{
    ElementType tmp = *a;
    *a = *b;
    *b = tmp;
}

输入样例:

3
12.3 34 -5

输出样例:

12.30
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

James_Xue_2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值