【问题标题】:Largest Occurrence Count最大出现次数
【发布时间】:2019-02-13 19:13:42
【问题描述】:

我是编程新手,被这个问题难住了,我尝试了几种不同的方法,但都没有成功。

实现(源代码)一个程序(将其命名为 LargestOccurenceCount),该程序从用户读取非零正整数值,找到最大值,并计算它的出现次数。假设输入以数字 0 结尾(作为停止循环的标记值)。程序应忽略任何负输入,并应继续读取用户输入,直到输入 0。程序应该显示最大值和出现的次数

Scanner reader = new Scanner(System.in);
int num = 0;
int array[] = null;

while ((num = reader.nextInt()) != 0) {
    System.out.println("Enter a positive integer (0 to quit): ");
    num = reader.nextInt();
    array[num] = num;
}

样品运行 1:

输入正整数(0 退出):3 4 5 -9 4 2 5 1 -5 2 5 0

最大值:5 出现次数:3次

程序应该输出输入的最大值以及在输入0之前输入的次数。

【问题讨论】:

  • 该样本运行中的最大数字确实是 5,并且确实出现了 3 次。有什么问题?
  • 你可能想要包括一些你的尝试——所以写作并不是一个真正的家庭作业。具体是什么给你带来了问题?迭代?存储计数?还有什么?
  • 我猜这是作业中给出的示例输出,而不是您的输出。如果它达到那么远,您的代码看起来会抛出 NPE。您需要向我们提供您的输出,我们不只是为您编写代码。

标签: java arrays loops


【解决方案1】:

你可以使用下面的sn-p:

// part of reading the values from command line and 
// putting them into this array is omitted
int[] array = ...;
int biggest = 0;
int occurance = 0;
for(int num : array) {
    if(num > biggest) {
        biggest = num;
        occurance = 0;
    }
    if(num == biggest) {
        occurance++;
    }
}
System.out.printf("Biggest number %s occured %s times.%n", biggest, occurance);

正如评论中提到的,我省略了您从命令行读取值的部分,因为这似乎不是您的问题。


另一种直接读取的解决方案,不需要数组并且全部在一个循环中:

Scanner scanner = new Scanner(System.in);
int biggest = 0;
int occurance = 0;
int num;

while (true) {
    System.out.print("Enter a number: ");
    // this may throw an Exception if the users input is not a number
    num = Integer.parseInt(scanner.nextLine());
    if(num == 0) {
        // user entered a 0, so we exit the loop
        break;
    }
    if(num > biggest) {
        biggest = num;
        occurance = 1;
    } else if(num == biggest) {
        biggest++;
    }
}
System.out.printf("Biggest number %s occured %s times.%n", biggest, occurance);

【讨论】:

  • 希望他们参加考试时你也会在场。 IMO 作业问题不应直接回答。它违背了家庭作业的目的。
  • 感谢您的帮助。我想我让这件事变得比需要的困难得多,并试图使用我范围之外的东西。
【解决方案2】:

将您的问题分成两部分。 1.找到最大值

int findLargest(int[] array) {
  assert array.length > 0;
  int result = array[0];
  for (int element : array) {
    if (element > result) {
      result = element;
    }
  }
  return result;
}
  1. ...并在数组中找到它
int countOccurences(int[] array, int value) {
  int occurences = 0;
  for (int element : array) {
    if (element == value) {
      occurences++;
    }
  }
  return occurences;
}

注意数组至少要有一个元素;

【讨论】:

  • 为什么要求数组至少有一个值?您的第一个方法不必要地检查第一个元素两次。
  • 就是这样。我想知道是否有人会注意到一项不必要的操作。这个问题最简单的解决方案是使用 Stream 和 max() 函数。
  • 希望他们参加考试时你也会在场。 IMO 作业问题不应直接回答。它违背了家庭作业的目的。
【解决方案3】:

如果没有要求只使用数组,您可以使用ArrayList 来存储用户输入

List<Integer> list = new ArrayList<>();
while ((num = reader.nextInt()) != 0) {
    System.out.println("Enter a positive integer (0 to quit): ");
    num = reader.nextInt();
    list.add(num);
}

那么如果你对stream api很熟悉的话,有一个相当简洁的解决方案:

Map.Entry<Integer, Long> lastEntry = list.stream()
            .collect(groupingBy(Function.identity(), TreeMap::new, counting()))
            .lastEntry();

System.out.println(
        "Largest value: " + lastEntry.getKey() +
        " Occurrences: " + lastEntry.getValue() + " times");

【讨论】:

  • 希望他们参加考试时你也会在场。 IMO 作业问题不应直接回答。它违背了家庭作业的目的。
  • @DaveNewton 我希望他能弄清楚这段代码,而不仅仅是复制/过去。但总的来说你是对的。我会考虑的..谢谢!
  • 感谢您的帮助。我想我让它变得比它需要的更复杂。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多