Skip to Content
CoursesCSC102การค้นหาและแสดงตำแหน่งในเมทริกซ์ 2 มิติ

การค้นหาและแสดงตำแหน่งในเมทริกซ์ 2 มิติ

โจทย์

Write a Java program that allows the user to input a number and then searches for that number in a given 2D matrix. The program should return all the indices (row and column) of the number if it appears more than once. If the number does not exist in the matrix, the program should indicate that the number is not present.

Input:

  • First line contains row and column of the matrix and the number for searching
  • Second line contains the members of the matrix

Output:

  • Display all the indices of the number in the matrix or a message indicating that the number is not present.
InputOutput
3 3 2
1 2 3
4 5 6
7 8 9
Indices of number 2: (0,1)
3 3 10
1 2 3
4 5 6
7 8 9
There is no number 10 in the matrix.

โค้ด

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); int numberToSearch = scanner.nextInt(); int indicesCount = 0; boolean[][] matrixLocation = new boolean[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int number = scanner.nextInt(); if (number == numberToSearch) { matrixLocation[i][j] = true; indicesCount++; } } } if (indicesCount == 0) { System.out.printf("There is no number %d in the matrix.", numberToSearch); } else { System.out.printf("Indices of number %d: ", numberToSearch); for (int x = 0; x < n; x++) { for (int y = 0; y < m; y++) { if (matrixLocation[x][y]) { System.out.printf("(%d,%d) ", x, y); } } } } } }

คำอธิบาย

1. การประกาศคลาสและรับข้อมูล

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); // จำนวนแถว int m = scanner.nextInt(); // จำนวนคอลัมน์ int numberToSearch = scanner.nextInt(); // ตัวเลขที่ต้องการค้นหา

2. การเตรียมตัวแปรสำหรับติดตามตำแหน่ง

int indicesCount = 0; // นับจำนวนครั้งที่พบ boolean[][] matrixLocation = new boolean[n][m]; // เก็บตำแหน่งที่พบ

3. การค้นหาและบันทึกตำแหน่ง

for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int number = scanner.nextInt(); if (number == numberToSearch) { // ถ้าพบตัวเลขที่ต้องการ matrixLocation[i][j] = true; // ทำเครื่องหมายตำแหน่งที่พบ indicesCount++; // เพิ่มตัวนับ } } }

4. การแสดงผล

if (indicesCount == 0) { // กรณีไม่พบตัวเลขที่ต้องการ System.out.printf("There is no number %d in the matrix.", numberToSearch); } else { // กรณีพบตัวเลขที่ต้องการ System.out.printf("Indices of number %d: ", numberToSearch); for (int x = 0; x < n; x++) { for (int y = 0; y < m; y++) { if (matrixLocation[x][y]) { System.out.printf("(%d,%d) ", x, y); } } } }

แผนภาพการทำงาน

การติดตามตำแหน่ง

เมทริกซ์ตัวอย่าง: 1 5 3 5 2 5 4 5 6 matrixLocation: 0 1 0 1 0 1 0 1 0 (1 = พบตัวเลขที่ต้องการ)

ตัวอย่างการทำงาน

ตัวอย่างที่ 1: พบตัวเลขหลายตำแหน่ง

Input: 3 3 5 1 5 3 5 2 5 4 5 6 การทำงาน: 1. พบ 5 ที่ตำแหน่ง (0,1) ✓ 2. พบ 5 ที่ตำแหน่ง (1,0) ✓ 3. พบ 5 ที่ตำแหน่ง (1,2) ✓ 4. พบ 5 ที่ตำแหน่ง (2,1) ✓ Output: Indices of number 5: (0,1) (1,0) (1,2) (2,1)

ตัวอย่างที่ 2: ไม่พบตัวเลข

Input: 2 2 7 1 2 3 4 การทำงาน: ไม่พบเลข 7 ในเมทริกซ์ Output: There is no number 7 in the matrix.
ปรับปรุงล่าสุด