HDU1157 POJ2338 Who's in the Middle
来源:互联网 发布:古希腊神话 知乎 编辑:程序博客网 时间:2024/05/29 15:11
问题链接:HDU1157 POJ2338 Who's in the Middle。基础级练习题,用C语言编写程序。
问题简述:输入n,然后输入n个整数,求这n个整数中大小位于中间的数。
问题分析:使用分治法,用求第k大数算法实现。
程序说明:另外一种解法更加简洁,参见以下链接。
参考链接:POJ2388 HDU1157 Who's in the Middle。
AC的C语言程序如下:
/* HDU1157 POJ2338 Who's in the Middle */#include <stdio.h>#define MAXN 10000int data[MAXN+1];int split(int a[], int low, int high){ int part_element = a[low]; for (;;) { while (low < high && part_element <= a[high]) high--; if (low >= high) break; a[low++] = a[high]; while (low < high && a[low] <= part_element) low++; if (low >= high) break; a[high--] = a[low]; } a[high] = part_element; return high;}// 非递归选择问题算法程序int selectmink(int a[], int low, int high, int k){ int middle; for(;;) { middle = split(a, low, high); if(middle == k) return a[k]; else if(middle < k) low = middle+1; else /* if(middle > k) */ high = middle-1; }}int main(void){ int n, ans, i; while(scanf("%d", &n) != EOF) { for(i=0; i<n; i++) scanf("%d", &data[i]); ans = selectmink(data, 0, n - 1, n / 2); printf("%d\n", ans); } return 0;}
1 0
- HDU1157 POJ2338 Who's in the Middle
- HDU1157:Who's in the Middle
- HDU1157 Who's in the Middle
- HDU1157 Who's in the Middle
- HDU1157 Who's in the Middle
- hdu1157 Who's in the Middle
- POJ2388 HDU1157 Who's in the Middle【中位数+排序】
- 快速排序浅谈——(解题报告)HDU1157和POJ2388---Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- Who's in the Middle
- ngx_slab_pool
- JAVA NIO之浅谈内存映射文件原理与DirectMemory
- 初步体验微信后台开发
- 使用webpack打包Vue工程
- 火车票购票问题2
- HDU1157 POJ2338 Who's in the Middle
- orcale 包,包体,函数,游标.(未完)
- iOS-webView上加载HTML视频不显示缩略图
- 人民币小写转换成大写
- 常见电脑截屏技巧汇总
- 从今天开始写博客
- ios::sync_with_stdio(false);
- java多聚合,少继承,低耦合,高内聚
- HDU-1576 A/B 【逆元模板题】