在Java中查找模式和不排序模式的频率



我是Java新手,有人给了我一个问题作为周末项目来解决。我遇到了一些麻烦,需要你的帮助。请理解我是一个初学者。如果我哪里说错了,请解释给我听。我希望有一天也能成为一名优秀的程序员。

我做了一个全面的搜索,并找到了答案,如"热图"或"数组列表",一些我可能不会被允许使用,因为我还没有学会它。

好的,题目是:

查找:1)模式,课堂中出现频率最高的标记。如果2个或更多的分数同样频繁地出现,那么这些分数中最高的是模式。

2) Mode Frequency:模式的频率。

假设班级有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出现了三次。

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

我想我用三个for循环弄得一团糟。我似乎想不出如何解决这个问题。

一种解决方案是将长度为101的数组初始化为0。这个数组将表示特定标记出现的次数。每次遇到一个特定的标记,就增加计数。然后,要找到模式,只需找到计数最高的索引。

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
        });
    }
}

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

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);
    }
}

最新更新