【问题标题】:Finding mode and frequency of mode without sorting in Java在Java中无需排序即可查找模式和模式频率
【发布时间】:2014-09-10 15:06:31
【问题描述】:

我是 Java 新手,作为一个周末项目,我收到了一个要解决的问题。我遇到了一些麻烦,希望得到您的帮助。请理解我是初学者。如果我在某个地方错了,请向我解释。我希望有一天也能成为一名优秀的程序员。

我进行了全面搜索,并找到了诸如“热图”或“数组列表”之类的答案,因为我还没有学会它,所以我可能不会被允许使用它。

好的,所以给我的问题是:

查找:1) 模式,班级中出现频率最高的标记。如果 2 个或更多标记同样频繁地出现,则这些标记中最高的就是众数。

2) 模式频率:模式的频率。

假设班级有10名学生,分数在0到100之间,不允许对分数进行排序。

这是我查找模式的代码:

void mode()
{
    int c[]=new int[10];
    int arr[]=new int[10];
    for (int i=0;i<10;i++)
    {   
        for (int j=i+1;j<10;j++)
        {
            for (int k=0;k<10;k++)
            {
                if (marks[i]!=arr[k])
                {               
                    if (marks[i]==marks[j])
                    {
                        c[i]++;
                        break;
                    }   
                }
            }
            arr[i]=marks[i];
        }
    }
    for (int k=0;k<10;k++)
    {
        System.out.println();
        System.out.println(c[k]);
    }
}

marks[] 是我输入的 int 数组,c[] 是计算数字出现的次数,arr[] 是一个数组,用于交叉检查该数字是否以前出现过.

假设输入的 10 个数字是 99、95、97、92、80、95、73、80、95、80。如您所见,95 和 80 出现了 3 次。

所以我的 c[] 应该有 {0, 2, 0, 0, 2, 0, 0, 0, 0, 0},但是当我运行它时,它是 {0, 2, 0, 0, 2, 1, 0, 1, 0, 0},这意味着我的程序没有与 arr[] 进行交叉检查。

我认为我使用三个 for 循环弄得一团糟。我似乎无法弄清楚如何解决它。

【问题讨论】:

  • 我想问一下为什么不允许排序?这本书或老师想在这里测试什么?
  • 不需要排序。请参阅@vandale 的回答。

标签: java arrays nested-loops frequency mode


【解决方案1】:
public class Loader
{

    // We suppose that the parameter is not null
    public static void mode_frequency(long collection[])
    {
        long frequencies[] = new long[collection.length];
        for (int i = 0, l = collection.length; i < l; i++)
        {
            for (int j = i; j < l; j++)
            {
                if (collection[i] == collection[j])
                {
                    ++frequencies[i];
                }
            }
        }

        // With your example {99, 95, 97, 92, 80, 95, 73, 80, 95, 80}
        // You should have somthing like {1, 3, 1, 1, 3, 2, 1, 2, 1, 1}
        //
        // As you can see, you just have to find the MAX frequency and then, print corresponding values from initial array
        long frequency = 0;
        for (int i = 0, l = frequencies.length; i < l; i++)
        {
            if (frequency < frequencies[i])
            {
                frequency = frequencies[i];
            }
        }
        // Print each mode
        for (int i = 0, l = collection.length; i < l; i++)
        {
            if (frequencies[i] == frequency)
            {
                System.out.println(collection[i]);
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        mode_frequency(new long[]
        {
            99,
            95,
            97,
            92,
            80,
            95,
            73,
            80,
            95,
            80
        });
    }

}

【讨论】:

    【解决方案2】:

    您可能希望使用一种算法,在该算法中,您使用辅助数组来累积各个标记的计数,然后在这些辅助数组中搜索频率最高且值最大的标记。考虑:

    package com.example.mode;
    
    public class Mode {
    
        public static void main(String[] args) {
    
            int[] marks = { 99, 95, 97, 92, 80, 95, 73, 80, 95, 80};
    
    
            //One algorithm .. insert marks and add occurances, keeping in order
    
            int[] unique  = new int[marks.length];
            int[] count = new int[marks.length];
            int maxUnique = 0;
    
            for (int i=0; i < marks.length ; i++) {
                int loc = -1;
                for (int j=0; j < maxUnique; j++) {
                    if (marks[i] == unique[j]) {
                        loc = j;
                        break;
                    }
                }
                if (loc == -1) {
                    loc = maxUnique;
                    unique[loc] = marks[i];
                    count[loc] = 0;
                    maxUnique++;
                }
                count[loc] = count[loc]+1;
            }
            int maxValue = unique[0];
            int maxCount = count[0];
            for (int j=1; j < maxUnique; j++) {
                if (count[j] > maxCount || (count[j] == maxCount && maxValue < unique[j]) ) {
                    maxValue = unique[j];
                    maxCount = count[j];
                }
            }
    
            System.out.println("Mode = " + maxValue + ", frequency = " + maxCount);
    
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      一种解决方案是将长度为 101 的数组初始化为零。该数组将表示特定标记出现的次数。每次遇到特定标记时,都会增加计数。然后要查找模式,您只需找到计数最高的索引即可。

      【讨论】:

        猜你喜欢
        • 2013-10-05
        • 2018-03-14
        • 2012-01-12
        • 2016-02-22
        • 1970-01-01
        • 2013-07-01
        • 2023-04-02
        • 2017-03-05
        • 1970-01-01
        相关资源
        最近更新 更多