Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.File;
  4.  
  5. import java.io.FileInputStream;
  6.  
  7. import java.io.FileWriter;
  8.  
  9. import java.io.IOException;
  10.  
  11. import java.util.Arrays;
  12. import java.util.Scanner;
  13.  
  14. public class Main {
  15.     private static int currentIndex = -1;
  16.  
  17.     private static Integer next(String numbers[]) {
  18.         ++currentIndex;
  19.         while (currentIndex < numbers.length && numbers[currentIndex].equals("")) {
  20.             ++currentIndex;
  21.         }
  22.         return currentIndex < numbers.length ? Integer.parseInt(numbers[currentIndex]) : null;
  23.     }
  24.  
  25.     static void Theme() {
  26.         System.out.println("В данной матрице подсчитать количество столбцов, у которых элементы расставлены в порядке возрастания.");
  27.     }
  28.  
  29.     static void outputMatrix(int[][] Matrix, int size) {
  30.         for (int i = 0; i < size; ++i, System.out.println())
  31.             for (int j = 0; j < size; ++j)
  32.                 System.out.print(Matrix[i][j] + " ");
  33.     }
  34.  
  35.     static void outputInFile(String result) throws IOException {
  36.         Scanner in = new Scanner(System.in);
  37.         int i, j;
  38.         String outputFileName;
  39.         System.out.println("Введите имя файла в который хотите вывести данные:");
  40.         outputFileName = in.nextLine();
  41.         outputFileName = outputFileName + ".txt";
  42.         File newFile = new File(outputFileName);
  43.         try {
  44.             boolean created = newFile.createNewFile();
  45.             if (created)
  46.                 System.out.println("Файл успешно создан.");
  47.         } catch (IOException ex) {
  48.             System.out.println(ex.getMessage());
  49.         }
  50.         FileWriter out = new FileWriter(outputFileName);
  51.         out.write(result);
  52.         out.close();
  53.     }
  54.  
  55.     static String getSortedColumnsIndexes(int[][] matrix) {
  56.         int size = matrix.length;
  57.         int[] sortedColumns = new int[size];
  58.         int columnsCount = 0;
  59.         for (int column = 0; column < size; column++) {
  60.             boolean sorted = true;
  61.             int row = 0;
  62.             while (sorted && row < size - 1) {
  63.                 if (matrix[row][column] > matrix[row + 1][column]) {
  64.                     sorted = false;
  65.                 }
  66.                 row++;
  67.             }
  68.             if (sorted) {
  69.                 sortedColumns[columnsCount] = column + 1;
  70.                 columnsCount++;
  71.             }
  72.         }
  73.  
  74.         String result = "Количество отсортированных столбцов столбцов: " + columnsCount + ". Номера  столбцов: ";
  75.  
  76.         for (int i = 0; i < columnsCount; i++) {
  77.             result += sortedColumns[i] + " ";
  78.         }
  79.  
  80.         return result;
  81.     }
  82.  
  83.     public static void main(String[] args) throws IOException {
  84.         Scanner in = new Scanner(System.in);
  85.         String NameOfFile;
  86.         Theme();
  87.         System.out.println("Введите имя файла, с которого хотите считать" + " информацию: ");
  88.         NameOfFile = in.nextLine();
  89.         NameOfFile = NameOfFile + ".txt";
  90.  
  91.         FileInputStream inFile = new FileInputStream(NameOfFile);
  92.         byte[] str = new byte[inFile.available()];
  93.         inFile.read(str);
  94.         String text = new String(str);
  95.         String[] numbers = text.split("\\D");
  96.  
  97.         int size = next(numbers);
  98.  
  99.         int[][] matrix = new int[size][size];
  100.         for (int i = 0; i < size; ++i) {
  101.             for (int j = 0; j < size; ++j) {
  102.                 matrix[i][j] = next(numbers);
  103.             }
  104.         }
  105.  
  106.         System.out.println("Исходная матрица:");
  107.         outputMatrix(matrix, size);
  108.  
  109.  
  110.  
  111.         String sortedColumns = getSortedColumnsIndexes(matrix);
  112.         System.out.println(sortedColumns);
  113.         outputInFile(sortedColumns);
  114.  
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement