首页 > 试题广场 >

中位数

[编程题]中位数
  • 热度指数:5873 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解

M给你一个长度为n的数组,我们定义median数为该数组从小到大排序后,下标为(n-1)/2的数字。下标从0开始,(n-1)/2表示整数除法,即向下取整。现在我们已经得到了一个初始的数组,我们希望这个数组的median数是一个给定数字x。所以我们需要加入一些数到数组中从而完成我们的目标。数组中的元素可以重复,请问,最少需要加入多少个数字才能达成这个目标。


输入描述:
第一行输入两个整数n x (1 <= n <= 500, 1 <= x <= 10^5)。

接下来一行有n个正整数表示初始的数组,用空格分开,范围是[1, 10^5]。


输出描述:
输出需要加入最少的元素个数才能够使得新数组的median数成为x。
示例1

输入

3 2
2 3 4

输出

1

说明

样例一中,加入1,得到1 2 3 4,那么median数的下标为(4 - 1)/2 = 1, median数为2。加一个数字就可以了。
示例2

输入

3 4
1 2 3

输出

4

说明

样例二中,加入4 5 6 7,得到1 2 3 4 5 6 7,median数为4。最少加4个数字。
import java.util.*;
public class Main{
    public static void main(String[]args){
        Scanner sc=new Scanner(System.in);
                //至少添加的次数,即结果
        int res=0;
        int nums=sc.nextInt();
        int target=sc.nextInt();
                //小于中位数的个数
        int min=0;
                //大于中位数的个数
        int max=0;
                //等于中位数的个数
        int same=0;
                //中位数是否存在
        boolean isExist=false;
        for(int i=0;i<nums;i++){
            int temp=sc.nextInt();
            if(temp>target){
                max++;
               
            }
            else if(temp<target){
                min++;
            }
            else if(temp==target){
                              //中位数已存在,保存多出来的个数
                  if(isExist){
                    same++;
                  }else{
                isExist=true;}
            }
        }
                //如果不存在中位数,添加中位数,次数加一
        if(isExist==false) res++;
                //如果max大于min的时候,记录差值为dif
//如果dif大于same时,中位数的左边要添加dif-same-1个数组
//举例:中位数为3  1 2 3 3 3 4 4 4 4 4 4 4
//因为中位数永远位于左边,所以可以少添加一个 
//如果dif小于same时,添加0
//举例: 中位数为3  1 2 2 3 3 3 3 4 4 4 4
        if(max>min){
            int dif=max-min;
            dif=dif>same?dif-same-1:0;
            res+=(dif);
        }
                //如果min大于max情况的时候 ,记录差值为dif
//如果dif大于same时 中位数的右边要添加dif-same个数字:举例 中位数为3  1 2 2 2 2 3 3 4 4 
                //如果dif小于等于same时 添加0:举例 中位数为3  1 2 2 2 3 3 3 3 4 
        else if(min>max){
            int dif=min-max;
            dif=dif>same?dif-same:0;
            res+=(dif);
        }
        
        System.out.print(res);
    }
}

发表于 2019-09-13 14:45:57 回复(0)
import java.util.*;

public class Main {

	public static void main(String[] args){
		
		Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int x = sc.nextInt();
        int[] array=new int[n];
        int left=0;
        int right=0;
        int xcount=0;
        int result=0;
        for(int i=0;i<n;i++)
        {
            int num = sc.nextInt();
            if(num==x)
                xcount++;
            else if(num<x)
                left++;
            else if(num>x)
                right++;
        }
        if(xcount==0)
        {
            xcount++;
            result++;
        }
        if(left+xcount<right)
        {
            result+=(right-(left+xcount));
            System.out.println(result);
            return;
        }
        else if(left>=right+xcount)
        {
            result+=(left-(right+xcount)+1);
            System.out.println(result);
            return;
        }
        else
        {
            System.out.println(result);
            return;
        }
	}
}

发表于 2019-08-30 12:53:29 回复(0)