Advertisement
emodev

Untitled

Feb 15th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5.  
  6. public class SomeMatrixTask {
  7.     public static void main(String[] args) throws IOException {
  8.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  9.         int rowN = Integer.parseInt(reader.readLine());
  10.         int colM = Integer.parseInt(reader.readLine());
  11.  
  12.         int[][] matrix = new int[rowN][colM];
  13.  
  14. //        Read n lines from console with numbers separated by space
  15.         for (int i = 0; i < rowN; i++) {
  16.             String[] line = reader.readLine().split(" "); // Split the line by space and make an array of the numbers
  17.  
  18.             for (int j = 0; j < colM; j++) {
  19.                 matrix[i][j] = Integer.parseInt(line[j]);  // On the current row of matrix(array) add new number M times
  20.  
  21.             }
  22.         }
  23.  
  24.  
  25.         int maxEvenCount = 0;
  26.         int[] mostEvensRow = null;
  27.  
  28. //        Iterate through matrix and check every number if its Even
  29.         for (int[] array : matrix) {
  30.             int counterEvens = 0;
  31.             for (int i : array) {
  32.                 if (i % 2 == 0) {
  33.                     counterEvens++;
  34.                 }
  35.             }
  36.             if (counterEvens > maxEvenCount) {  // If even numbers on this row are more than the previous one we save the row because we have to print it in the end
  37.                 maxEvenCount = counterEvens;
  38.                 mostEvensRow = array;
  39.             }
  40.         }
  41.  
  42. //        If there's no even numbers at all just print this message and terminate the program
  43.         if (mostEvensRow == null) {
  44.             System.out.println("No exist even numbers in the matrix");
  45.             return;
  46.         }
  47.  
  48. //        Print the row with most even numbers separated by space
  49.         for (int i : mostEvensRow) {
  50.             System.out.print(i + " ");
  51.         }
  52.  
  53.  
  54. //        Примерни входове
  55. //        3
  56. //        3
  57. //        4 2 5
  58. //        5 3 7
  59. //        6 2 2
  60. //        Резултат: 6 2 2
  61. //
  62. //        2
  63. //        2
  64. //        1 1
  65. //        3 3
  66. //        Резултат: No exist even numbers....
  67. //
  68. //      3
  69. //      3
  70. //      4 3 5
  71. //      5 2 2
  72. //      6 7 9
  73. //      Резултат: 5 2 2
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement