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 ...
随机推荐
- 移动端CSS小结
Meta 标签 <meta name="viewport" content="width=device-width, user-scalable=no, init ...
- Microduino-W5500
2014-06-13, Microduino 公布了全新的以太网模块Microduino-W5500 ,模块基于WIZnet以太网芯片,拥有独特的全硬件TCP/IP协议栈. attachment_id ...
- python 多线程中同步的小样例
#!/usr/bin/python # -*- coding: UTF-8 -*- # 在一个资源池中.获取资源 # Author: zhang # Date: 2015-7-27 import ti ...
- 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用
责任链模式的具体应用 1.业务场景 生产车间中使用的条码扫描,往往一把扫描枪需要扫描不同的条码来处理不同的业务逻辑,比如,扫描投入料工位条码.扫描投入料条码.扫描产出工装条码等,每种类型的条码位数 ...
- 竞赛中经常使用的C++写法
首先是构造函数,重载 #include <iostream> #include <cstdio> #include <cstring> #include <s ...
- base64和图片互转
pom.xml添加 <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> <dependen ...
- Android之——经常使用手机号码功能
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47374415 有些Android手机中会带有一些经常使用号码的功能,比方订餐电话. ...
- Axure安装fontawesome字体
http://www.fontawesome.com.cn/ 下载后,双击安装字体提示 不是有效的字体,百度 ..解决方法: 任务管理器--服务-- MpsSvc-Windows Firewall ...
- Apsara Clouder专项技能认证:实现调用API接口 (笔记)
- 20170111 ABAP技术小结(全半角转换)
DATA: it_po LIKE it_alv OCCURS 0 WITH HEADER LINE.************************************************** ...