Problem A: Median Value

Time Limit: 1 Sec Memory Limit: 128 MB

Submit: 874 Solved: 307

[Submit][Status][Web Board]

Description

Figure out the median of a floating point number array. If the size of an array is an odd number, the median is the middle element after arranging all the array elements from lowest value to highest value; If there is an even number of elements, then there is no single middle value, so the median is the mean of the two middle values.

Input

The input may contain several test cases.

The first line of each test case is an integer n (n >= 1), representing the size of the array.

The second line of each test case contains all the elements in the array.

Input is terminated by EOF.

Output

For each test case, output the median , which must be formatted as a floating point number with exactly two digit after the decimal point.

Sample Input

6

5.0 4.0 3.0 2.0 11.0 3.0

11

5.0 6.0 222.0 23.0 23.0 4.0 2.0 5.0 99.0 1.0 8.0

Sample Output

3.50

6.00

问题描述:

该题为水题,只需注意数组中长度。数组长度为奇数时,输出中间值即可;若数组长度为偶数,输出中间两数和的一半。

另外注意输出保留小数点位两位,采用cout<

#include <iostream>
#include <iomanip>
using namespace std;
void sort(float* shu, int length) {
int i, j;
float temp;
for (i = 0; i<length - 1; ++i) {
for (j = 0; j<length - i - 1; j++) {
if (shu[j + 1]<shu[j]) {
temp = shu[j + 1];
shu[j + 1] = shu[j];
shu[j] = temp;
}
}
}
}
int main() {
int n;
float a[101];
while (cin >> n)
{
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, n);
if (n % 2 == 0)
cout << fixed << setprecision(2) << ((a[n / 2] + a[n / 2 - 1]) / 2) << endl;
else
cout << fixed << setprecision(2) << a[n / 2] << endl;
}
return 0;
}
/**************************************************************
Problem: 1009
User: 20151000332
Language: C++
Result: Accepted
Time:12 ms
Memory:1268 kb

Median Value的更多相关文章

  1. No.004:Median of Two Sorted Arrays

    问题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  2. [LeetCode] Find Median from Data Stream 找出数据流的中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  3. [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  4. Applying vector median filter on RGB image based on matlab

    前言: 最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习 ...

  5. 【leetcode】Median of Two Sorted Arrays

    题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...

  6. Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing

    B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...

  7. 【leedcode】 Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  8. Find Median from Data Stream

    常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...

  9. 数据结构与算法(1)支线任务8——Find Median from Data Stream

    题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...

  10. leetcode-【hard】4. Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

随机推荐

  1. 使用MySQL Workbench进行数据库设计——MySQL Workbench用法总结

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/48318877 本文出自[我是干勾鱼的博客] 1 简单介绍 MySQL Workb ...

  2. Office EXCEL 如何保留一位小数,并且单击这个单元格的时候没有一大串小数

    左侧有一列数据,即便我设置单元格格式,把小数位数设为1,看上去的确四舍五入,保留一位小数了,但是实际上我鼠标双击任意单元格,还是原来的数值,这样的数据如果是要发给别人的,肯定不好   如果进行选择性粘 ...

  3. python菜鸟日记1

    1. 在__init__(self,...)初始化函数中.定义对象的属性,这些属性不用所有写在括号里,括号里的表示定义的时候须要赋初始值的一些属性,而其它不用初始值的属性.能够 直接卸载代码块中,se ...

  4. #Virtual hosts #Include conf/extra/httpd-vhosts.conf 开启就不能启动apache

    #Virtual hosts#Include conf/extra/httpd-vhosts.conf我只要把其中任何一个开启就是吧#去掉就启动不了apache.怎么回事error.log是这样的ht ...

  5. 【SICP练习】149 练习4.5

    练习4-5 原文 Exercise 4.5. Scheme allows an additional syntax for cond clauses, ( => ). If evaluates ...

  6. 积跬步,聚小流------Bootstrap学习记录(3)

    响应式作为Bootstrap的一大特色.栅格系统可谓是功不可没,既然如此,那我们就来看一下栅格系统是怎样帮助bootstrap实现响应式布局的呢? 1.什么是栅格系统 我们能够从Bootstrap的官 ...

  7. OpenCV图像处理篇之图像平滑

    图像平滑算法 图像平滑与图像模糊是同一概念,主要用于图像的去噪.平滑要使用滤波器.为不改变图像的相位信息,一般使用线性滤波器,其统一形式例如以下: %20\Large%20g(i,j)=\sum_{k ...

  8. 亲測Mysql表结构为InnoDB类型从ibd文件恢复数据

    客户的机器系统异常关机,重新启动后mysql数据库不能正常启动,重装系统后发现数据库文件损坏,悲催的是客户数据库没有进行及时备份,仅仅能想办法从数据库文件其中恢复,查找资料,试验各种方法,确认以下步骤 ...

  9. android使用两种方式注冊receiver

    1. package com.example.myreceiver; import android.os.Bundle; import android.app.Activity; import and ...

  10. HTTP要点概述:四,HTTP方法

    使用HTTP协议的时候,客户端可以通过HTTP方法告知服务器自己请求的意图. 看了这篇文章以后,谁再说HTTP方法只有GET和POST,你的眼睛是用来吃饭的嘛! 一,GET:获取资源 GET用来请求访 ...