Uva10107 what is the median?

Source: Internet
Author: User

What is the median?
The problemmedian plays an important role in the world of statistics. by definition, it is a value which divides an array into two equal parts. in this problem you are to determine the current median of some long integers.

Suppose, we have five numbers {1, 3, 6, 2, 7 }. in this case, 3 is the median as it has exactly two numbers on its each side. {1, 2} and {6, 7 }.

If there are even number of values like {1, 3, 6, 2, 7, 8}, only one value cannot split this array into equal two parts, so we consider the average of the middle values {3, 6 }. thus, the median will be (3 + 6)/2 = 4.5. in this problem, you have to print only the integer part, not the fractional. as a result, according to this problem, the median will be 4!

Input the input file consists of series of integers x (0 <= x <2 ^ 31) and total number of integers n is less than 10000. the numbers may have leading or trailing spaces. output for each input print the current value of the median. sample Input
1346070502
Sample output
12334274
Sadi Khan
2001-04-01
The question is hard to understand.

#include <cstdio>#include <algorithm>using std:: sort;int arr[10002], id;int main(){int n;while(scanf("%d", &n) != EOF){arr[id++] = n;sort(arr, arr + id);if(id & 1) printf("%d\n", arr[id / 2]);else printf("%d\n", (arr[id / 2] + arr[id / 2 - 1]) / 2);}return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.