0

这就是我想要的:
让用户输入任意数量的数字,直到输入非数字(您可能假设数字少于 100)。查找最常输入的数字。(如果有多个,打印所有。)
示例输出:
输入:5
输入:4
输入:9
输入:9
输入:4
输入:1
输入:a
最常见的:4, 9
我明白了在我的代码中,我设法找出哪些是最常见的数字。但是,我不想一遍又一遍地打印出相同的数字;上面的例子: 最常见的: 4, 9, 9, 4
需要做什么?

public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String[] input = new String[100];
    System.out.print("Input: ");
    input[0] = in.readLine();
    int size = 0;
    for (int i = 1; i < 100 && isNumeric(input[i-1]); i++) {
            System.out.print("Input: ");
            input[i] = in.readLine();
            size = size + 1;
    }
    /*for (int i = 0; i < size; i++) { //testing
        System.out.println(input[i]);
    }*/
    int numOccur;
    int[] occur = new int[size];
    for(int i = 0; i < size; i++) {
        numOccur = 0;
        for (int j = 0; j < size; j++) {
            if(input[i].equals(input[j])) {
                numOccur = numOccur + 1;
            }
        }
        occur[i] = numOccur;
        //System.out.println(numOccur); //testing
    }
    int maxOccur = 0;
    for(int i = 0; i < size; i++) {
        if(occur[i] > maxOccur) {
            maxOccur = occur[i];
        }
    }
    //System.out.println(maxOccur); //testing
    for (int i = 0; i < size && !numFound; i++) {
        if(occur[i] == maxOccur) {
           System.out.println(input[i]);
        }
    }

}

//checks if s is an in, true if it is an int
public static boolean isNumeric (String s) {
    try {
        Integer.parseInt(s);
        return true; //parse was successful
    } catch (NumberFormatException nfe) {
        return false;
    }
}

找到了解决方案!

String[] mostCommon = new String[size];
    int numMostCommon = 0;
    boolean numFound = false;
    for (int i = 0; i < size; i++) {
        int isDifferent = 0;
        if (occur[i] == maxOccur) {
            for (int j = 0; j < size; j++) {
                if (!(input[i].equals(mostCommon[j]))) {
                    isDifferent = isDifferent + 1;
                }
            }
            if (isDifferent == size) {
                mostCommon[numMostCommon] = input[i];
                numMostCommon = numMostCommon + 1;
            }
        }
    }
    for (int i = 0; i < numMostCommon - 1; i++) {
        System.out.print("Most common: " + mostCommon[i] + ", ");
    }
    System.out.println(mostCommon[numMostCommon - 1]);
4

4 回答 4

1

您可以为此使用哈希表来存储频率,因为限制非常小,即小于 100。伪代码如下:
vector<int> hash(101)
cin>>input
if(isnumeric(input))
hash[input]++
else{
max=max_element(hash.begin(),hash.end());
for(int i=0;i<100;i++)
if(hash[i]==max)
print i
}

于 2012-04-06T20:18:11.273 回答
0

您可以使用Set并存储已打印的值。

于 2012-04-06T19:58:38.173 回答
0

这样的事情呢?

public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    Map<string,int> numberLookup = new HashMap<string,int>();
    Boolean doContinue = true;
    while (doContinue)
    {
        System.out.print("Input: ");
        String input = in.readLine();
        if (isNumeric(input))
        {
            if (!numberLookup.containsKey(input))
                numberLookup.put(input,1);
            else
                numberLookup.put(input, numberLookup.get(input) + 1);
        }
        else
            doContinue = false;
    }

    maxOccur = numberLookup.values().max();
    System.out.print("These numbers were all entered " + maxOccur + " times:");
    Iterator it = numberLookup.entrySet().iterator();
    while (it.hasNext())
    {
        (Map.Entry)it.next();
        System.out.println(pairs.getKey());
    } 
}

抱歉,我是 C# 人,没有 Java 编译器,所以这可能需要一些调整。

于 2012-04-06T20:27:40.320 回答
0
    Set<Integer> uniqueMaxOccur = new HashSet<Integer>();  
    for (int i = 0; i < size ; i++) {
        if(occur[i] == maxOccur) {
            //System.out.println(input[i]);
            uniqueMaxOccur.add(input[i]);
        }
    }

并显示集合中的值

于 2012-04-06T20:08:50.110 回答