返回数字的输出不符合预期?

时间:2015-12-04 17:43:11

标签: java arrays

  

给定一个数字数组作为输入,返回在输入中出现最大次数的数字。

我不知道我正在创建的新数组的大小。我假设大小是两倍。为了分开我划分的数字,数字将它转换为modulas并添加到新数组。之后我找到了最大数字在新的数组中。但我没有得到预期的输出。

@Edit根据提到的答案我编辑了代码。现在出现的问题是我的数组c值没有被引用。(我的意思是我把它们用数字分开但是我得到了相同的数字数组返回q

  

任何人都可以指导我做错了什么吗?

 public int frequentDigit(int[] numbers)
 {
   int q=0;
   int c[]=seperateDigits(numbers);
   int p=findMax(c);
   for(int i=0;i<c.length;i++)
   {
    if(p==i)
    {
        //b1=true;
        q=numbers[i];
       break;
    }
   } 

   return q;

 }      

 int[] seperateDigits(int[] numbers) 
 {     
  int count=0;

  for (int i = 0; i < numbers.length; i++) 
  {
   int aNumber = numbers[i];
   if(aNumber==0)           //@Edit
   count++;
  else
  {
     while (aNumber > 0) 
    {
      int aDigit = aNumber % 10;
      System.out.println(aDigit);
       count++;


     aNumber = aNumber / 10;
    }
  }

 }
 int c[] = new int[count];

 for (int i = 0; i < numbers.length; i++) 
 {
  int aNumber = numbers[i];
  if(aNumber==0)           //@Edit
     c[i]=aNumber;
  else
  {
     while (aNumber > 0) 
    {
      int aDigit = aNumber % 10;
      //System.out.println(aDigit);
       //count++;
      c[i]=aDigit;             //@Edit
     //System.out.println(c[i]);  //@Edit

     aNumber = aNumber / 10;
    }
  }

 }
 return c;
}

    int findMax(int c[]) 
    {
        int max = c[0];
        int max_i = 0;
        for (int i = 1; i < c.length; i++)
        {
            if (c[i] > max) 
            {
                max = c[i];
                max_i = i;
            }
        }
        return max_i;
    }

输出

       Parameters          |  Actual Output  |  Expected Output
---------------------------|-----------------|-----------------
{24,27,30,31,34,37,40,42}  |     40          |        3

2 个答案:

答案 0 :(得分:3)

这是一个组合版本。它可以处理零值(计为单个0数字)和负值(符号被忽略)。空数组返回-1,空数组抛出NullPointerException

private static int findMostFrequentDigit(int ... input) {
    int[] digitCount = new int[10];
    for (int value : input)
        do {
            digitCount[Math.abs(value % 10)]++;
        } while ((value /= 10) != 0);
    int maxCount = 0, maxDigit = -1;
    for (int i = 0; i < 10; i++)
        if (digitCount[i] > maxCount) { // or >= to find the last max-digit
            maxCount = digitCount[i];
            maxDigit = i;
        }
//  printDebug(input, digitCount, maxCount);
    return maxDigit;
}

测试

System.out.println(findMostFrequentDigit(24,27,30,31,34,37,40,42));
System.out.println(findMostFrequentDigit(12345, 54321, 24, 159, 2468, 98765, 0, 1020304050));
System.out.println(findMostFrequentDigit(123, -654, 879));
System.out.println(findMostFrequentDigit(0));

输出

3
0
1
0

要获得良好的调试输出,请使用此方法并取消注释上面的printDebug()行:

private static void printDebug(int[] input, int[] digitCount, int maxCount) {
    System.out.println(Arrays.toString(input));
    String format = "%" + (Integer.toString(maxCount).length() + 1) + "d";
    System.out.print("  Digit:");
    for (int i = 0; i < 10; i++)
        System.out.printf(format, i);
    System.out.println();
    System.out.print("  Count:");
    for (int i = 0; i < 10; i++)
        System.out.printf(format, digitCount[i]);
    System.out.println();
    format = "%" + (Integer.toString(maxCount).length() + 1) + "s";
    System.out.print("  Max:  ");
    for (int i = 0; i < 10; i++)
        System.out.printf(format, digitCount[i] == maxCount ? "^" : "");
    System.out.println();
}

输出

[24, 27, 30, 31, 34, 37, 40, 42]
  Digit: 0 1 2 3 4 5 6 7 8 9
  Count: 2 1 3 4 4 0 0 2 0 0
  Max:         ^ ^          
[12345, 54321, 24, 159, 2468, 98765, 0, 1020304050]
  Digit: 0 1 2 3 4 5 6 7 8 9
  Count: 6 4 5 3 5 5 2 1 2 2
  Max:   ^                  
[123, -654, 879]
  Digit: 0 1 2 3 4 5 6 7 8 9
  Count: 0 1 1 1 1 1 1 1 1 1
  Max:     ^ ^ ^ ^ ^ ^ ^ ^ ^
[0]
  Digit: 0 1 2 3 4 5 6 7 8 9
  Count: 1 0 0 0 0 0 0 0 0 0
  Max:   ^                  

答案 1 :(得分:2)

我看到的一些事情......

1)您的frequentDigit方法应该返回int,而不是int[]

2)你需要使用长度为10的数组,如@Andreas在评论中提到的那样。

3)您的findMax方法返回最频繁数字出现的次数,而不是数字本身。要捕获数字,您需要跟踪max和与之对应的i值。您无需检查c的长度,for循环就是为您做的。

int findMax(int c[]) {
    int max = c[0];
    int max_i = 0;
    for (int i = 1; i < c.length; i++) {
        if (c[i] > max) {
            max = c[i];
            max_i = i;
        }
    }
    return max_i;
}

4)for中的seperateDigits循环(旁边:“分开”,而非“单独”)正在做一些动机但不正确的神秘事物。

int c[] = new int[10];
//System.out.println(c.length);
for (int i = 0; i < numbers.length; i++) {
    int aNumber = numbers[i];
    if (aNumber == 0)
        c[0]++;
    else {
        while (aNumber > 0) {
            int aDigit = aNumber % 10;
            System.out.println(aDigit);

            c[aDigit]++;
            System.out.println(c[aDigit]);

            aNumber = aNumber / 10;
        }
    }
}

整个辣酱玉米饼馅

package xyz;

public class DigitCounter {

    public static void main(String[] args) {
        DigitCounter me = new DigitCounter();
        System.out.println(me.frequentDigit(24, 27, 30, 31, 34, 37, 40, 42));
        System.out.println(me.frequentDigit(12345, 54321, 24, 159, 2468, 98765,
                0, 1020304050));
        System.out.println(me.frequentDigit(123, -654, 879));
        System.out.println(me.frequentDigit(0));
    }

    public int frequentDigit(int... numbers) {
        int c[] = separateDigits(numbers);
        int p = findMax(c);
        return p;
    }

    int[] separateDigits(int[] numbers) {
        int c[] = new int[10];
        for (int i = 0; i < numbers.length; i++) {
            int aNumber = numbers[i];
            if (aNumber == 0)
                c[0]++;
            else {
                while (aNumber > 0) {
                    int aDigit = aNumber % 10;
                    c[aDigit]++;
                    aNumber = aNumber / 10;
                }
            }
        }
        return c;
    }

    int findMax(int c[]) {
        int max = c[0];
        int max_i = 0;
        for (int i = 1; i < c.length; i++) {
            if (c[i] > max) {
                max = c[i];
                max_i = i;
            }
        }
        return max_i;
    }
}