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 ...
随机推荐
- Codeforces Round #277 (Div. 2)---C. Palindrome Transformation (贪心)
Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input sta ...
- hdoj 1203 I NEED A OFFER! 【另类01背包】【概率背包】
题意:... 策略:动态规划. 由于是求至少能得到一个offer的概率,那我们能够反着求.求得不到一个offer的概率.最后用1减去就好了. 代码: #include<string.h> ...
- Binder系列8—如何使用Binder(转)
一.Native层Binder 源码结构: ClientDemo.cpp: 客户端程序 ServerDemo.cpp:服务端程序 IMyService.h:自定义的MyService服务的头文件 IM ...
- 算法导论学习之线性时间求第k小元素+堆思想求前k大元素
对于曾经,假设要我求第k小元素.或者是求前k大元素,我可能会将元素先排序,然后就直接求出来了,可是如今有了更好的思路. 一.线性时间内求第k小元素 这个算法又是一个基于分治思想的算法. 其详细的分治思 ...
- 关于java及多线程
http://www.w3cschool.cc/java/java-multithreading.html
- [英语学习]王秒同学《21天TED英语精练团》
第一个分享: Chris Anderson的TED's secret to great public speaking(英音). There's no single formula for a gre ...
- 32位与4G内存限制
32位有4G内存限制,好像人所共知.但这个32位是指32位的CPU还是32位的操作系统? 答案是,都是.内存限制,是操作系统和硬件(CPU,也许还有内存控制器)共同制约的.CPU对应的是寻址物理地址, ...
- ABAP WEBRFC
通过WEBRFC实现在网页下载SMW0上传的文件 FUNCTION zhr_download_test. *"---------------------------------------- ...
- RK3288 make otapackage 出错的问题【转】
本文转载自:http://blog.csdn.net/u010439962/article/details/51734631 Installed file list: out/target/produ ...
- ACTION 关联表之间查询语句 SQL语句写法
/** EquUseRecord * @author cll * @return * @右边菜单中的使用记录操作 */ public String QueryAllEquUserecordAllInf ...