Median and average (source: JLOJ2332)

Median and average (source: JLOJ2332)

Description
usually puts the number in the middle of the n sorted numbers into the "median", here is a little more detailed, if n is an odd number, then there is only one middle number, that is, the "median" However, if n is an even number, there are two middle numbers. We also call the average of these two numbers the "median". The next task is to determine whether the median is large or the average of all numbers is large.

Input
has only one line, several integers, separated by a space between the two integers, and the input ends with 0. The range of each integer is -1000 ~ 1000 (including -1000 and 1000), and the number of integers entered does not exceed 2000.

Output
has only one line. If the median is greater than the average, then Yes is output, otherwise No.

Sample Input
200 100 -100 300 400 -200 0

Sample Output
Yes

Problem solving idea: It is
necessary to pay attention to the situation of the median when the integer number is odd or even, and the result of the average and median calculation may be floating-point data, and the variables that receive the average and median need to be set It is floating point.
The use of conditional expressions in this question can reduce the amount of code.

C reference program:

#include <stdio.h>
#include <stdlib.h>

int compar(const void* a, const void* b) {
    
    
	return *(int*)a - *(int*)b;
}

int main() {
    
    
	int arr[2001];
	int len = 0, sum = 0;
	double avg, mid;
	
	while (scanf("%d", &arr[len]) && arr[len]) {
    
    
		sum += arr[len];
		len++;
	}
	
	qsort(arr, len, sizeof(int), compar);
	
	avg = (sum * 0.1) / len;
	mid = len % 2 == 0 ? ((arr[len / 2] + arr[len / 2 - 1]) / 2.0) : (arr[len / 2]);
	
	printf("%s", mid > avg ? "Yes" : "No");
	
	return 0;
} 

C++ reference program:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    
    
	vector<int> arr;
	int value, sum = 0;
	double avg, mid;
	
	while (cin >> value && value) {
    
    
		arr.emplace_back(value);
		sum += value;
	}
	
	sort(arr.begin(), arr.end());
	
	int len = arr.size();
	
	avg = (sum * 1.0) / len;
	mid = len % 2 ? (arr[len / 2]) : ((arr[len / 2] + arr[len / 2 - 1]) / 2.0);
	
	cout << (mid > avg ? "Yes" : "No");
	
	return 0;
}

Java reference program:

import java.util.*;

public class test {
    
    

	public static void main(String[] args) {
    
    
		Scanner in = new Scanner(System.in);
		
		int[] arr = new int[2001];
		int val = 1, sum = 0, LEN = 0;
		double mid, avg;
		
		while (val != 0) {
    
    
			val = in.nextInt();
			arr[LEN] = val;
			sum += val;
			LEN++;
		}
		LEN -= 1;
		
		for (int i = 0; i < LEN - 1; i++) {
    
    
			for (int j = i + 1; j < LEN; j++) {
    
    
				if (arr[i] > arr[j]) {
    
    
					int c = arr[i];
					arr[i] = arr[j];
					arr[j] = c;
				}
			}
		}
		
		avg = sum / LEN;
		if (LEN % 2 == 0) {
    
    
			mid = (arr[LEN / 2] + arr[LEN / 2 - 1]) / 2;
		} else {
    
    
			mid = arr[LEN / 2];
		}
		
		System.out.println(mid > avg ? "Yes" : "No");
	}

}

complete.

Guess you like

Origin blog.csdn.net/weixin_45711556/article/details/108738911