Advertisement
Ligh7_of_H3av3n

02. Positions Of

May 16th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. package Lekciq;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class PositionsOf {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.  
  10.  
  11.         // Read the dimensions of the matrix
  12.         int rows = scanner.nextInt();
  13.         int cols = scanner.nextInt();
  14.  
  15.         // Read the matrix
  16.         int[][] matrix = new int[rows][cols];
  17.         for (int i = 0; i < rows; i++) {
  18.             for (int j = 0; j < cols; j++) {
  19.                 matrix[i][j] = scanner.nextInt();
  20.             }
  21.         }
  22.  
  23.         // Read the number to find in the matrix
  24.         int numberToFind = scanner.nextInt();
  25.  
  26.         // Find and print positions of the number in the matrix
  27.         boolean found = false;
  28.         for (int i = 0; i < rows; i++) {
  29.             for (int j = 0; j < cols; j++) {
  30.                 if (matrix[i][j] == numberToFind) {
  31.                     System.out.println(i + " " + j);
  32.                     found = true;
  33.                 }
  34.             }
  35.         }
  36.  
  37.         // Print "not found" if the number does not appear in the matrix
  38.         if (!found) {
  39.             System.out.println("not found");
  40.         }
  41.     }
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement