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的理解吧. 关于数据库索引及其优化,更多可参见此文:http://www.cnblogs.com/pkuoliver/archive/2011/08/17/mass- ...
- c++之NVI手法
non-virtual interface(NVI)手法:令用户通过public non-virtual成员函数间接调用private virtual函数,将这个non-virtual函数称为virt ...
- ElasticSearch(5.5.2)在java中的使用
ElasticSearch(5.5.2)在java中的使用 https://blog.csdn.net/didiaodeabing/article/details/79310710 pom.xml: ...
- 【C】字符串,字符和字节(C与指针第9章)
C语言没有一种显式的数据类型是字符串的. C语言存储字符串:字符串常量(不能改动).字符数组或动态分配的内存(能够改动) *************************************** ...
- 开发:异常收集之 DB2建表相关问题
第一次用DB2数据库,因为考虑到建表语句可能不一样,所以採用手动建表的办法.一个个字段去填.并勾选主键.最后发现创建失败.看了下系统生成的sql语句 sql语句例如以下: CREATE TABLE F ...
- 块状元素的text-align对齐属性
能够为块状元素(div,h1,h2,form等)内容设置位置text-align:center,left;right;
- 安装BIRT Chart Engine的时候,提示Cannot complete the install because one or more required items could not be
http://wiki.eclipse.org/BIRT_Update_Site_URL 每个eclipse对应的BIRT版本 help-install new software: http://do ...
- windows下使用mingw和msys编译GOTOBLAS和OpenBLAS
在windows下利用msys编译openBLAS若遇到错误提示: gcc: CreateProcess : No such file or directory 问题原因参考:http://www.c ...
- high-level operations on files and collections of files
11.10. shutil — High-level file operations — Python 3.6.5 documentation https://docs.python.org/3/li ...
- cocos2dx笔记1:概述
1.核心的类和功能 CCDirector gameLoop,实现场景绘制.多个场景之间切换控制.控制游戏的停止,暂停,等生命周期. CCScene 场景类,每一个场景能够理解为一个游戏镜头.状态 CC ...