Java中的2D数组[关闭]

c9x0cxw0  于 2023-11-15  发布在  Java
关注(0)|答案(3)|浏览(178)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
2天前关闭。
Improve this question
给定一个整数二维数组A[1..5, 1..5],找到最大的K,使得在数组的任何一行中,都存在大于或等于K的元素。

  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.PrintStream;
  4. import java.util.Scanner;
  5. public class Solution {
  6. public static void main(String[] args) throws FileNotFoundException {
  7. Scanner scan = new Scanner(new File("input.txt"));
  8. PrintStream out = new PrintStream(new File("output.txt"));
  9. // Create a 2D array to store the input data
  10. int[][] array = new int[5][5];
  11. // Read data from the input file into the 2D array
  12. for (int i = 0; i < 5; i++) {
  13. for (int j = 0; j < 5; j++) {
  14. array[i][j] = scan.nextInt();
  15. }
  16. }
  17. // Find the maximum K
  18. int maxK = Integer.MIN_VALUE;
  19. for (int i = 0; i < 5; i++) {
  20. for (int j = 0; j < 5; j++) {
  21. if (array[i][j] > maxK) {
  22. maxK = array[i][j];
  23. }
  24. }
  25. }
  26. // Write the result to the output file
  27. out.println(maxK);
  28. }
  29. }

字符串
我写了一个程序,错误地解决了这个问题,我需要帮助。就我对这个任务的理解,我需要找到阈值,在该阈值以上有等于或大于这个阈值的数字,但我不知道如何解决它。
下面是一个测试用例,用于验证我的代码是否错误地解决了问题:
input.txt:

  1. 6 7 9 4 5
  2. 7 0 2 5 6
  3. 5 7 -7 1 7
  4. 5 2 2 8 4
  5. 1 3 3 4 5


预期产出:

  1. 5


然而,我的程序给出了以下输出:9
请帮助我解决这个问题。

cotxawn7

cotxawn71#

你的maxK是数组的整体最大值(即所有元素)。但是对于你的任务,你必须找到每行的最大值。然后K是这些元素中的最小值。

  1. int k = Integer.MAX_VALUE;
  2. for (int i = 0; i < numberOfRows; i++) {
  3. int m = Integer.MIN_VALUE;
  4. for (int j = 0; j < numberOfColumns; j++) {
  5. int v = scanner.nextInt();
  6. if (v > m) m = v; //find the maximum of the current row
  7. }
  8. if (m < k) k = m; //compare the maximum to the current k
  9. }
  10. out.println(k);

字符串

mccptt67

mccptt672#

这个问题很简单,你只需要找到每一行的max数,然后找到所有max数的min值。
这个问题可以用时间O(N)来解决。

  1. public static int findLargest(int[][] arr) {
  2. int k = Integer.MAX_VALUE;
  3. for (int row = 0; row < arr.length; row++) {
  4. int maxRow = Integer.MIN_VALUE;
  5. for (int col = 0; col < arr[row].length; col++)
  6. maxRow = Math.max(maxRow, arr[row][col]);
  7. k = Math.min(k, maxRow);
  8. }
  9. return k;
  10. }

字符串

展开查看全部
cbeh67ev

cbeh67ev3#

  • 我写了一个程序错误地解决了这个问题 *

首先,请确保关闭 Closeable 对象。
一个典型的方法是使用 *try-with-resources**语句。

  1. try (Scanner scan = new Scanner(new File("input.txt"))) {
  2. try (PrintStream out = new PrintStream("output.txt")) {
  3. }
  4. }

字符串

  • "..

遍历 * 数组 *,并在每个元素上遍历。

  1. // Find the maximum K
  2. int maxK = array[0][0];
  3. for (int[] r : array)
  4. for (int v : r) if (v >= maxK) maxK = v;


这里是完整的重构因子。

  1. try (Scanner scan = new Scanner(new File("input.txt"))) {
  2. // Create a 2D array to store the input data
  3. int[][] array = new int[5][5];
  4. // Read data from the input file into the 2D array
  5. for (int i = 0; i < 5; i++) {
  6. for (int j = 0; j < 5; j++) {
  7. array[i][j] = scan.nextInt();
  8. }
  9. }
  10. // Find the maximum K
  11. int maxK = array[0][0];
  12. for (int[] r : array)
  13. for (int v : r) if (v >= maxK) maxK = v;
  14. try (PrintStream out = new PrintStream("output.txt")) {
  15. // Write the result to the output file
  16. out.println(maxK);
  17. }
  18. }


为了演示,这里有一个使用 stream 的例子。

  1. try (Scanner scan = new Scanner(new File("input.txt")).useDelimiter("\\R")) {
  2. // Find the maximum K
  3. int maxK
  4. = scan.tokens()
  5. .mapToInt(x -> Stream.of(x.trim().split(" +"))
  6. .mapToInt(Integer::parseInt)
  7. .max()
  8. .getAsInt())
  9. .max()
  10. .getAsInt();
  11. try (PrintStream out = new PrintStream("output.txt")) {
  12. // Write the result to the output file
  13. out.println(maxK);
  14. }
  15. }

展开查看全部

相关问题