求自定类型元素序列的中位数
来源:互联网 发布:注册淘宝店流程 编辑:程序博客网 时间:2024/05/21 08:48
/*
求自定类型元素序列的中位数
其中给定集合元素存放在数组A[]
中,正整数N
是数组元素个数。该函数须返回N
个A[]
元素的中位数,其值也必须是ElementType
类型。
*/
#include <stdio.h>#define MAXN 10typedef float ElementType;ElementType Median( ElementType A[], int N );int main (){ ElementType A[MAXN]; int N, i;while(scanf("%d", &N) != EOF){for ( i=0; i<N; i++ ) scanf("%f", &A[i]); printf("%.2f\n", Median(A, N)); } return 0;}void HeapAdjust(ElementType array[], int i, int nlength){int nChild;ElementType nTemp;for (; 2 * i + 1 < nlength; i = nChild){nChild = 2 * i + 1;//得到子节点中较大的节点if (nChild < nlength - 1 && array[nChild + 1] > array[nChild])nChild++;//如果较大的子节点大于父节点那么把较大的子节点往上移动,替换它的父节点if (array[i] < array[nChild]){nTemp = array[i];array[i] = array[nChild];array[nChild] = nTemp;}elsebreak;}}void Heapsort(ElementType array[], int length){int i;//调整序列的前半部分元素,调整完之后第一个元素是序列的最大元素ElementType temp;for (i = length / 2 - 1; i >= 0; i--)HeapAdjust(array, i, length);//从最后一个元素开始对序列进行调整,不断的缩小调整的范围直到第一个元素for (i = length - 1; i >= 0; i--){//把第一个元素和当前的最后一个元素交换//保证当前的最后一个位置的元素都是现在这个序列中最大的temp = array[0];array[0] = array[i];array[i] = temp;//不断缩小调整heap的范围,每一次调整完毕保证第一个元素是当前序列的最大值HeapAdjust(array, 0, i);}}ElementType Median(ElementType A[], int N){ Heapsort(A, N);return A[N / 2];}
0 0
- 4-11 求自定类型元素序列的中位数
- 求自定类型元素序列的中位数 PAT
- 求自定类型元素序列的中位数
- 4-11 求自定类型元素序列的中位数
- 4-11 求自定类型元素序列的中位数*
- 求自定类型元素序列的中位数
- 6-11 求自定类型元素序列的中位数
- PAT练习基础编程题目之求自定类型元素序列的中位数
- PAT-C-4-11 求自定类型元素序列的中位数 (25分)
- 4-11 求自定类型元素序列的中位数 (25分)
- 4-11 求自定类型元素序列的中位数 (25分)
- PTA 4-11 求自定类型元素序列的中位数 (25分)
- PTA 求自定类型元素序列的中位数,冒泡超时,改为希尔排序
- 4-11 求自定类型元素序列的中位数 (25分)
- 4-11 求自定类型元素序列的中位数 (25分)
- PAT基础题 4-11 求自定类型元素序列的中位数
- 6-11 求自定类型元素序列的中位数(25 分)
- 6-11 求自定类型元素序列的中位数(25 分)
- maven的Generating Project in Batch mode 卡住问题
- ID3算法
- STL复杂算法
- css中的div布局之巧用div
- 中国象棋
- 求自定类型元素序列的中位数
- 「Emacs org-mode(一)」用Org-mode实现GTD
- linux 常用命令
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
- 样本间的距离或者相似度度量方法
- <香港科技大学html+css+js课堂笔记>week1--CSS部分
- 《PCANet: A Simple Deep Learning Baseline for Image Classification》
- 五边形
- (二)操作mongodb数据库