Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Main {
- private static int currentIndex = -1;
- private static Integer next(String numbers[]) {
- ++currentIndex;
- while (currentIndex < numbers.length && numbers[currentIndex].equals("")) {
- ++currentIndex;
- }
- return currentIndex < numbers.length ? Integer.parseInt(numbers[currentIndex]) : null;
- }
- static void Theme() {
- System.out.println("В данной матрице подсчитать количество столбцов, у которых элементы расставлены в порядке возрастания.");
- }
- static void outputMatrix(int[][] Matrix, int size) {
- for (int i = 0; i < size; ++i, System.out.println())
- for (int j = 0; j < size; ++j)
- System.out.print(Matrix[i][j] + " ");
- }
- static void outputInFile(String result) throws IOException {
- Scanner in = new Scanner(System.in);
- int i, j;
- String outputFileName;
- System.out.println("Введите имя файла в который хотите вывести данные:");
- outputFileName = in.nextLine();
- outputFileName = outputFileName + ".txt";
- File newFile = new File(outputFileName);
- try {
- boolean created = newFile.createNewFile();
- if (created)
- System.out.println("Файл успешно создан.");
- } catch (IOException ex) {
- System.out.println(ex.getMessage());
- }
- FileWriter out = new FileWriter(outputFileName);
- out.write(result);
- out.close();
- }
- static String getSortedColumnsIndexes(int[][] matrix) {
- int size = matrix.length;
- int[] sortedColumns = new int[size];
- int columnsCount = 0;
- for (int column = 0; column < size; column++) {
- boolean sorted = true;
- int row = 0;
- while (sorted && row < size - 1) {
- if (matrix[row][column] > matrix[row + 1][column]) {
- sorted = false;
- }
- row++;
- }
- if (sorted) {
- sortedColumns[columnsCount] = column + 1;
- columnsCount++;
- }
- }
- String result = "Количество отсортированных столбцов столбцов: " + columnsCount + ". Номера столбцов: ";
- for (int i = 0; i < columnsCount; i++) {
- result += sortedColumns[i] + " ";
- }
- return result;
- }
- public static void main(String[] args) throws IOException {
- Scanner in = new Scanner(System.in);
- String NameOfFile;
- Theme();
- System.out.println("Введите имя файла, с которого хотите считать" + " информацию: ");
- NameOfFile = in.nextLine();
- NameOfFile = NameOfFile + ".txt";
- FileInputStream inFile = new FileInputStream(NameOfFile);
- byte[] str = new byte[inFile.available()];
- inFile.read(str);
- String text = new String(str);
- String[] numbers = text.split("\\D");
- int size = next(numbers);
- int[][] matrix = new int[size][size];
- for (int i = 0; i < size; ++i) {
- for (int j = 0; j < size; ++j) {
- matrix[i][j] = next(numbers);
- }
- }
- System.out.println("Исходная матрица:");
- outputMatrix(matrix, size);
- String sortedColumns = getSortedColumnsIndexes(matrix);
- System.out.println(sortedColumns);
- outputInFile(sortedColumns);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement