zoukankan      html  css  js  c++  java
  • 解题报告——POJ 2623

    Sequence Median
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 15792   Accepted: 4409

    Description

    Given a sequence of N nonnegative integers. Let's define the median of such sequence. If N is odd the median is the element with stands in the middle of the sequence after it is sorted. One may notice that in this case the median has position (N+1)/2 in sorted sequence if sequence elements are numbered starting with 1. If N is even then the median is the semi-sum of the two "middle" elements of sorted sequence. I.e. semi-sum of the elements in positions N/2 and (N/2)+1 of sorted sequence. But original sequence might be unsorted. 

    Your task is to write program to find the median of given sequence.

    Input

    The first line of input contains the only integer number N - the length of the sequence. Sequence itself follows in subsequent lines, one number in a line. The length of the sequence lies in the range from 1 to 250000. Each element of the sequence is a positive integer not greater than 2^32 - 1 inclusive.

    Output

    You should print the value of the median with exactly one digit after decimal point.

    Sample Input

    4
    3
    6
    4
    5
    

    Sample Output

    4.5

    Hint

    Huge input,scanf is recommended.
     
    ------------------------------------------------------------------------------------------------
    这道题可以直接用快排进行计算,然后直接计算中位数。
    需要注意的小地方如下:
    1. 数组类型:采用long long的原因是为了计算sum,这个值超过了unsinged的范围
    2. 浮点数输出:对于大数字(2^32 - 1),不能用浮点数表示,而应该将浮点数拆分为整数和小数进行拼装。
    -----------------------------------------------------------------------------------------------
     1 #include <cstdio>
     2 #include <algorithm>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     long long a[250005];
     9     int n;
    10     scanf("%d", &n);
    11     for(int i = 0; i < n; i++)
    12     {
    13         scanf("%lld", &a[i]);
    14     }
    15     sort(a, a + n);
    16     if(n & 1)
    17     {
    18         printf("%lld.0
    ", a[n / 2]);
    19     }
    20     else
    21     {
    22         long long sum = a[n / 2] + a[n / 2 - 1];
    23         printf("%lld", sum / 2);
    24         if(sum & 1)
    25         {
    26             printf(".5
    ");
    27         }
    28         else
    29         {
    30             printf(".0
    ");
    31         }
    32     }
    33 
    34     return 0;
    35 }
     
  • 相关阅读:
    解决easy ui 1.4datebox控件不能清空的问题
    easy ui 1.4的NumberBox,失去焦点后不能再次输入小数点
    使用easy ui过程中资料(网址)总结
    解决easy ui两次请求服务器的问题
    JQuery EasyUI中datagrid的使用
    java对象和xml相互转换
    mybatisPuls初步了解
    Spring Cloud Alibaba系列
    artemis.http.client1.2 导致springboot日志不输出
    当我们在谈论爬虫时我们在谈论什么(一)
  • 原文地址:https://www.cnblogs.com/codingpenguin/p/4293950.html
Copyright © 2011-2022 走看看