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

セットを使用して、すでに印刷されている値を保存できます。

于 2012-04-06T19:58:38.173 に答える
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 に答える
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 に答える