Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Arrays;
- public class SomeMatrixTask {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int rowN = Integer.parseInt(reader.readLine());
- int colM = Integer.parseInt(reader.readLine());
- int[][] matrix = new int[rowN][colM];
- // Read n lines from console with numbers separated by space
- for (int i = 0; i < rowN; i++) {
- String[] line = reader.readLine().split(" "); // Split the line by space and make an array of the numbers
- for (int j = 0; j < colM; j++) {
- matrix[i][j] = Integer.parseInt(line[j]); // On the current row of matrix(array) add new number M times
- }
- }
- int maxEvenCount = 0;
- int[] mostEvensRow = null;
- // Iterate through matrix and check every number if its Even
- for (int[] array : matrix) {
- int counterEvens = 0;
- for (int i : array) {
- if (i % 2 == 0) {
- counterEvens++;
- }
- }
- 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
- maxEvenCount = counterEvens;
- mostEvensRow = array;
- }
- }
- // If there's no even numbers at all just print this message and terminate the program
- if (mostEvensRow == null) {
- System.out.println("No exist even numbers in the matrix");
- return;
- }
- // Print the row with most even numbers separated by space
- for (int i : mostEvensRow) {
- System.out.print(i + " ");
- }
- // Примерни входове
- // 3
- // 3
- // 4 2 5
- // 5 3 7
- // 6 2 2
- // Резултат: 6 2 2
- //
- // 2
- // 2
- // 1 1
- // 3 3
- // Резултат: No exist even numbers....
- //
- // 3
- // 3
- // 4 3 5
- // 5 2 2
- // 6 7 9
- // Резултат: 5 2 2
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement