简体   繁体   English

如何避免重复相同的数字?

[英]How can I avoid repetition of the same number?

This is what I want:这就是我要的:
Let the user enter as many numbers as they want until a non number is entered (you may assume there will be less than 100 numbers).让用户输入任意数量的数字,直到输入非数字为止(您可以假设数字少于 100 个)。 Find the most frequently entered number.查找最常输入的数字。 (If there are more than one, print all of them.) (如果有多个,请打印所有。)
Example output:示例 output:
Input: 5输入:5
Input: 4输入:4
Input: 9输入:9
Input: 9输入:9
Input: 4输入:4
Input: 1输入:1
Input: a输入:一个
Most common: 4, 9最常见:4、9
I have got to the point in my code where I have managed to find out which are the most common numbers.在我的代码中,我已经到了设法找出哪些是最常见数字的地步。 However, I don't want to print out the same number over and over again;但是,我不想一遍又一遍地打印出相同的数字; example from above: Most common: 4, 9, 9, 4上面的例子:最常见的:4, 9, 9, 4
What needs to be done?需要做什么?

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

Found the solution!找到解决方案!

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

you could use the hash table for this to store the frequenceis as the limit is very less ie less than 100. pseudo code would be like:您可以为此使用 hash 表来存储频率,因为限制非常小,即小于 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
}

You can use aSet and store the values already printed.您可以使用Set并存储已打印的值。

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

and display the values in the set并显示集合中的值

What about something like this?像这样的事情怎么样?

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

Sorry, I'm a C# person and don't have a Java compiler on me, so this might need some tweaking.抱歉,我是 C# 人,身上没有 Java 编译器,所以这可能需要一些调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM