【问题标题】:testcase failing for -- Mean median mode question asked in competitive programming测试用例失败——竞争性编程中提出的平均中位数模式问题
【发布时间】:2021-05-11 06:49:43
【问题描述】:

我在公司的一次入学考试中得到了以下问题。除 4 个测试用例外,所有测试用例均通过。有人可以尝试找出可能的情况,这可能会失败。问题及解决方法如下:

均值、中位数和众数

给定“n”个整数,求它们的平均中位数和众数。 要求您填写一个接受输入整数 'input1' (1input2[] 包含 'input1' 整数并返回 output1 作为平均值 output2 作为中位数,output3 作为众数。

平均值和中位数必须正确到小数点后六位。

平均: 定义为数组中所有数字的平均值。

中位数: 定义为数组的中间元素。

如果n是偶数,中位数是两个中间元素的平均值 如果 n 为奇数,则中位数为数组的中间元素。

注意:为了找到中位数,数组中的元素必须按数字顺序列出 从小到大。

模式: 定义为数组中频率最高的数字。 如果许多数字具有相同的最高频率,则该模式由下式计算 打破平局以支持最小的数字。

输入规范:

Input1 : 1

Input2:整数输入数组。

输出规格:

返回 output1(double) 变量作为平均值。

返回 output2(double) 变量作为中位数。

返回 output3(int) 变量作为模式。

示例测试用例 1:

输入1: 3

输入 2: {1,2,3}

输出:2.000000,2.000000,1

示例测试用例 2:

输入1: 5

输入 2: {41, 18467, 6334, 26500, 19169 }

输出: 14102.200000,18467.000000,41

package algo;
import java.util.*;

public class MeanMedianMode 
{
    // Function for calculating mean
    public static double findMean(int n , int a[])
    {
        int sum = 0;
        for (int i = 0; i < n; i++)

        sum += a[i];

    return (double)sum / (double)n;
}

public static double findMedian(int n, int a[])
{
    // First we sort the array
    Arrays.sort(a);

    // check for even case
    if (n % 2 != 0)
        return (double)a[n / 2];

    return (double)(a[(n - 1) / 2] + a[n / 2]) / 2.0;
}

public static int findMode(int n, int a[] ) {
    int maxValue=0, maxCount=0;

    for (int i = 0; i < a.length; ++i) {
        int count = 0;
        for (int j = 0; j < a.length; ++j) {
            if (a[j] == a[i]) ++count;
        }
        if (count >  maxCount) {
            maxCount = count;
            maxValue = a[i];
        }
    }

   return maxValue;
 }

// Driver code
public static void main(String args[])
{
    int a[] = { 41, 18467, 6334, 26500, 19169 };
    int n = a.length;

    System.out.println("Mean = " + String.format("%.6f", findMean(n, a)));
    System.out.println("Median = " + String.format("%.6f", findMedian(n, a)) );
    System.out.println("Mode = " + findMode(n, a));
}
}

编辑:是否存在双峰、三峰数组的任何场景,例如 {1,1,2,2} 和 {1,1,2,2,3,3},其中模式可以是 {1,2} 或 { 1,2,3} 。我问这个是因为在这种情况下返回类型将是 int[],但有问题特别提到返回类型必须是 int。

【问题讨论】:

  • 我想说这个问题在findMode。您似乎没有处理多个具有相同最高频率的数字的情况。
  • @Johnny Moop 但在这种情况下 int[ ] 将是返回类型
  • @ASharma7 不行,你必须返回最小值
  • 问题特别提到返回类型必须是int
  • @JohnnyMopp 这就是问题所在,均值和中值均破并开始给出负值。请在答案中添加这个

标签: java algorithm


【解决方案1】:

问题出在findMode。您不处理有多个具有相同最高频率的数字的情况。问题陈述说:

如果许多数字具有相同的最高频率,则通过打破平局以支持最小的数字来计算模式。

最简单的解决方案是仅在模式值低于之前的模式值时存储模式值 - 将模式值初始化为可能的最高值。

public static int findMode(int n, int a[] ) {
    int maxValue = Integer.MAX_VALUE; // Note that a[0]+1 would also work
    int maxCount = 0;
    
    for (int i = 0; i < a.length; ++i) {
        int count = 0;
        for (int j = 0; j < a.length; ++j) {
            if (a[j] == a[i]) ++count;
        }
        if (count >= maxCount && a[i] < maxValue) {
            maxCount = count;
            maxValue = a[i];
        }
    }
    
    return maxValue;
}

请注意,您的算法效率低下,因为它每次出现在列表中时都会计算相同的数字。比如1在列表中出现100次,就会统计1出现的频率100次。

编辑:好的,因为你sort 数组,这不是问题。

我能想到的唯一另一件事是findMean 中存在整数溢出。 int 可以容纳的最大值是 2,147,483,647。由于数组可能很大(最多 1000 个元素),int sum 很可能无法保存该值。试试改成double sum = 0;

【讨论】:

  • 我同意低效率。但是对于多个数字请在您的机器中执行我的代码以输入 {2,2,1,1,3,3} ,我在我的机器中执行,它给出了 1 作为答案
  • 我用 {3,3,2,2,1,1} , {2,2,3,3,1,1} 尝试了我的代码,但我仍然得到 1 作为模式
  • 如果我先调用 findMedian() 然后调用 findMode() ,输出每次都会出现 1。
  • 如果我执行,findMode() 只有 2 或 3 根据输入来。
  • 是的,我只是在看那个。由于您在findMedian 中调用sort,因此您的代码将起作用。
猜你喜欢
  • 2021-09-15
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多