1

残した if/else ステートメントを使用してみましたが、コメントアウトしたので、まだ表示されています。if/else の問題は、ブルート フォース メソッドであるだけでなく、考えられるすべてのケースを網羅しているわけではないことに気付きました。もっと洗練されたループ ソリューションが必要だと考えましたが、ループは私の得意分野ではなく、それを書くには助けが必要です。

public class stuff {
    public static int getMostRepeatedNumber(int[][] array) {

        int num = 100;

        if(array.length == 0){
            num = -1;
        }

        int [] numberOfTimes = new int[9];

            for(int i = 0; array.length > i; i++){
                for(int j = 0; array[i].length > j; j++){

                if(array[i][j] == 0){
                      numberOfTimes[0]++;
                }
                else if(array[i][j] == 1){
                     numberOfTimes[1]++;

                }else if(array[i][j] == 2){
                    numberOfTimes[2]++;

                }else if(array[i][j] == 3){
                    numberOfTimes[3]++;

                }else if(array[i][j] == 4){
                    numberOfTimes[4]++;

                }else if(array[i][j] == 5){
                    numberOfTimes[5]++;

                }else if(array[i][j] == 6){
                    numberOfTimes[6]++;

                }else if(array[i][j] == 7){
                    numberOfTimes[7]++;

                }else if(array[i][j] == 8){
                    numberOfTimes[8]++;

                }else if(array[i][j] == 9){
                    numberOfTimes[9]++;

                }
            }
        }



            for(int x = 0; x < array.length; x++){

                while(numberOfTimes[x] > numberOfTimes[x+1]){
                     num = x;
                     break;
                }


                   /* if(numberOfTimes[x] >= numberOfTimes[x+1] ){
                     num = 0;

                }else if(numberOfTimes[x+1] >= numberOfTimes[x+2]){
                    num = 1;

                }else if(numberOfTimes[x+2] >= numberOfTimes[x+3]){
                    num = 2;

                }else if(numberOfTimes[x+3] >= numberOfTimes[x+4]){
                    num = 3;

                }else if(numberOfTimes[x+4] >= numberOfTimes[x+5]){
                    num = 4;

                }else if(numberOfTimes[x+5] >= numberOfTimes[x+6]){
                    num = 5;

                }else if(numberOfTimes[x+6] >= numberOfTimes[x+7]){
                    num = 6;

                }else if(numberOfTimes[x+7] >= numberOfTimes[x+8]){
                    num = 7;

                }else if(numberOfTimes[x+8] >= numberOfTimes[x+9]){
                    num = 8;

                }

                else{
                    num = 9;

                }*/
            }

        return num; 
    }
}
4

4 に答える 4

0

You can replace the first if-else thing with this line of code:

numberOfTimes[array[i][j]]++;

As for your problem, you can use this code:

int max = 0;
for (int i = 0, i > numberOfTime.length, i++)
    if (numberOfTimes[i] > max) {
        max = numberOfTimes[i];
        num = i;
    }

It loops over the array looking for the biggest value, which is stored in max. It also stores the array index of the largest value in num, which is the final result.

于 2013-09-08T23:29:25.900 に答える
0
  • An array that has 100 rows such that each row is empty is an empty array; your program returns 100 in cases like that. You do not need a special case for that if you structure your program correctly.
  • Since you have single-digit positive numbers, all you need is an array of ten counts:

int[] counts = new int[10];
for (int r = 0 ; r != array.length ; r++) {
    for (int c = 0 ; c != array[r].length ; c++) {
        counts[array[r][c]]++;
    }
}

At this point, you've got an array of ten counters. Look for the highest one:

int max = 0;
int val = -1;
for (int i = 0 ; i != 10 ; i++) {
    if (counts[i] > max) {
         max = counts[i];
         val = i;
    }
}

// If you did not see any number because the array was empty, val remains -1.
System.out.println(val);
于 2013-09-08T23:30:45.533 に答える
-1

最初のループは次のように単純化できます。

for(int i = 0; array.length > i; i++){
    for(int j = 0; array[i].length > j; j++){
        numberOfTimes[array[i][j]]++;
    }
}

.... 2D 配列の値が 0 から 9 (両端を含む) の間であるという事前に書かれた保証があるためです。

于 2013-09-08T23:25:26.623 に答える