Median Value
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的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- Applying vector median filter on RGB image based on matlab
前言: 最近想看看矢量中值滤波(Vector median filter, VMF)在GRB图像上的滤波效果,意外的是找了一大圈却发现网上没有现成的code,所以通过matab亲自实现了一个,需要学习 ...
- 【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 ...
- 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 ...
- 【leedcode】 Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- Find Median from Data Stream
常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...
- 数据结构与算法(1)支线任务8——Find Median from Data Stream
题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...
- 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 ...
随机推荐
- 查看MySQL系统变量的命令
用了好长时间mysql,却没有用心记住一些有用的东西,加油! mysql> SHOW VARIABLES; +---------------------------------+-------- ...
- 一起talk C栗子吧(第一百回:C语言实例--使用信号量进行进程间同步与相互排斥一)
各位看官们.大家好,上一回中咱们说的是进程间同步与相互排斥的样例,这一回咱们说的样例是:使用信号量进行进程间同步与相互排斥. 闲话休提,言归正转.让我们一起talk C栗子吧! 看官们,信号量是由著名 ...
- 如何删除Windows 7的保留分区
Windows 7的保留分区可以删除,但是必须小心.启动到Windows 7,运行具有管理员权限的CMD.exe,然后输入:diskpartsel disk 0list volsel vol 0 (你 ...
- [React] {svg, css module, sass} support in Create React App 2.0
create-react-app version 2.0 added a lot of new features. One of the new features is added the svgr ...
- Fp关联规则算法计算置信度及MapReduce实现思路
说明:參考Mahout FP算法相关相关源代码. 算法project能够在FP关联规则计算置信度下载:(仅仅是单机版的实现,并没有MapReduce的代码) 使用FP关联规则算法计算置信度基于以下的思 ...
- 说说Android应用的persistent属性(转)
1 启动persistent应用 在Android系统中,有一种永久性应用.它们对应的AndroidManifest.xml文件里,会将persistent属性设为true,比如: <appli ...
- 【JS】JavaScript引擎的内部执行机制
近期在复习JavaScript,看到setTimeout函数时.想起曾经刚学时,在一本书上看过setTimeout()里的回调函数执行的间隔时间有昌不是后面设置的值.曾经没想太多.网上看了JS大 ...
- Windows下的Jupyter Notebook 的介绍(写给新手)(图文详解)
不多说,直接上干货! Windows下的Python 3.6.1的下载与安装(适合32bits和64bits)(图文详解) Windows下的Jupyter Notebook 安装与自定义启动(图文详 ...
- sizeof、strlen
一.sizeof sizeof(...)是运算符,sizeof操作符的结果类型是size_t.它在头文件里typedef为unsigned int类型.是以字节为单位进行计数的.所以位域成员不 能用s ...
- swift语言初见
下面是swift得基础语法部分内容 // main.swift // helloSwift // // Created by cyteven on 14-7-23. // Copyright ...