HDU1157 POJ2338 Who's in the Middle

问题链接:HDU1157 POJ2338 Who‘s
in the Middle
。基础级练习题,用C语言编写程序。

题意简述:输入n,然后输入n个整数,求这n个整数中大小位于中间的数。

问题分析:使用分治法,用求第k大数算法实现。

AC的C语言程序如下:

/* HDU1157 POJ2338 Who's in the Middle */

#include <stdio.h>

#define MAXN 10000

int 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;
}

HDU1157 POJ2338 Who's in the Middle

时间: 2024-10-29 20:19:19

HDU1157 POJ2338 Who's in the Middle的相关文章

HDU1157 Who&#39;s in the Middle

Who's in the Middle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9411    Accepted Submission(s): 4538 Problem Description FJ is surveying his herd to find the most average cow. He wants to k

Who&#39;s in the Middle[HDU1157]

Who's in the Middle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16668    Accepted Submission(s): 7471 Problem Description FJ is surveying his herd to find the most average cow. He wants to k

垂直居中小记 line-height table vertical-align:middle

垂直居中分两种情况:1.父元素高度确定的单行文本        2.以及父元素高度确定的多行文本. 1.垂直居中-父元素高度确定的单行文本的竖直居中的方法是通过设置父元素的 height 和 line-height高度一致来实现的,即此时单行文本的行高line-height=height(父元素块高度).(height: 该元素的高度,line-height: 顾名思义,行高(行间距),指在文本中,行与行之间的 基线间的距离 ). line-height 与 font-size 的计算值之差,在

利用伪元素对块级元素应用vetical-align:middle使之垂直居中

vetical-align的功能是使行内元素垂直对齐. 可能的值 baseline 默认.元素放置在父元素的基线上. sub 垂直对齐文本的下标. super 垂直对齐文本的上标 top 把元素的顶端与行中最高元素的顶端对齐 text-top 把元素的顶端与父元素字体的顶端对齐 middle 把此元素放置在父元素的中部. bottom 把元素的顶端与行中最低的元素的顶端对齐. text-bottom 把元素的底端与父元素字体的底端对齐. length   % 使用 "line-height&qu

(hdu step 1.3.8)Who&#39;s in the Middle(排序)

题目: Who's in the Middle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2938 Accepted Submission(s): 1109   Problem Description FJ is surveying his herd to find the most average cow. He wants to k

vertical-align:middle和overflow:hidden;

如果想让一个div或一张图片相对于整个页面居中,用vertical-align:middle可以很简单地解决. 在设置了 width 或 height 的div中,写上overflow:hidden;的话,超出宽度或高度的部分,就隐藏了.就是说,这个overflow:hidden;属性可以保证div的高度或宽度不变.div里添加的东西再多,高度或宽度也不变.超出的部分隐藏.

BZOJ 2653: middle

2653: middle Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1536  Solved: 855[Submit][Status][Discuss] Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b]之间,右端点在[c,d]之间的子序列中,最大的中位数. 其中a<b<c<d. 位置

POJ 2388 Who&#39;s in the Middle(水~奇数个数排序求中位数)

题目链接:http://poj.org/problem?id=2388 题目大意: 奇数个数排序求中位数 解题思路:看代码吧! AC Code: 1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 int main() 5 { 6 int n; 7 while(scanf("%d",&n)!=EOF) 8 { 9 int na[n+1]; 10 for(int i=0; i

G - Who&#39;s in the Middle

Description FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median' cow gives: half of the cows give as much or more than the median; half give as much or less. Given an odd number of cows N (1 <= N < 10,0