Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6.     public static void printAdjacency(int[][] matrix){
  7.  
  8.         String[][] matrixVector = new String[matrix.length+1][matrix[0].length+1];
  9.  
  10.         for(int row = 0; row < matrixVector.length; row++)
  11.         {
  12.  
  13.             for(int column = 0; column < matrixVector[row].length; column++)
  14.             {
  15.  
  16.  
  17.                 if(row == 0 && column > 0)
  18.                 {
  19.                     matrixVector[row][column] = String.format("x%d",column);
  20.                 }
  21.  
  22.                 if(row > 0 && column == 0)
  23.                 {
  24.                     matrixVector[row][column] = String.format("x%d",row);
  25.                 }
  26.  
  27.                 if(row > 0 && column >0)
  28.                 {
  29.  
  30.                     matrixVector[row][column] = Integer.toString(matrix[row - 1][column - 1]);
  31.  
  32.                 }
  33.  
  34.             }
  35.  
  36.         }
  37.  
  38.         matrixVector[0][0] = "";
  39.  
  40.         for(int row = 0; row < matrixVector.length; row++)
  41.         {
  42.  
  43.             for(int column = 0; column < matrixVector[row].length; column++)
  44.             {
  45.  
  46.                 System.out.printf("%5s",matrixVector[row][column]);
  47.  
  48.             }
  49.             System.out.println();
  50.  
  51.         }
  52.  
  53.  
  54.     }
  55.  
  56.  
  57.     public static void inputAdjacency(int[][] matrix) {
  58.         Scanner reader = new Scanner(System.in);
  59.         for (int row = 0; row < matrix.length; row++) {
  60.  
  61.             for (int column = 0; column < matrix[row].length; column++) {
  62.                 System.out.printf("Вес вектора x%d - x%d\n", row + 1, column + 1);
  63.                 matrix[row][column] = reader.nextInt();
  64.                 printAdjacency(matrix);
  65.             }
  66.  
  67.         }
  68.     }
  69.  
  70.  
  71.     public static int amountOfRib(int[][] matrix)
  72.     {
  73.  
  74.         int result = 0;
  75.  
  76.         for(int row = 0; row < matrix.length; row++)
  77.         {
  78.  
  79.             for(int column = 0; column < matrix[row].length; column++)
  80.             {
  81.  
  82.                 if(matrix[row][column] != 0){result++;}
  83.  
  84.             }
  85.  
  86.         }
  87.         return result;
  88.  
  89.  
  90.     }
  91.  
  92.     public static void adjacencyToRib(int[][] matrix)
  93.     {
  94.  
  95.         String[][] ribList = new String[amountOfRib(matrix)][3];
  96.         int i = 0;
  97.         int j = 0;
  98.         for(int row = 0; row < matrix.length; row++)
  99.         {
  100.  
  101.             for(int column = 0; column < matrix[row].length; column++)
  102.             {
  103.  
  104.                 if(matrix[row][column] != 0)
  105.                 {
  106.                     ribList[i][j] = String.format("x%d",row+ 1);
  107.                     j++;
  108.                     ribList[i][j] = String.format("x%d",column+1);
  109.                     j++;
  110.                     ribList[i][j] = Integer.toString(matrix[row][column]);
  111.                     i++; j = 0;
  112.  
  113.  
  114.                 }
  115.  
  116.             }
  117.  
  118.         }
  119.  
  120.  
  121.         for(int row = 0; row < ribList.length; row++)
  122.         {
  123.  
  124.             for(int column = 0; column < ribList[row].length; column++)
  125.             {
  126.  
  127.                 System.out.printf("%5s",ribList[row][column]);
  128.  
  129.             }
  130.             System.out.println();
  131.  
  132.         }
  133.  
  134.  
  135.     }
  136.  
  137.  
  138.     public static void main(String[] args) {
  139.  
  140.  
  141.         int[][] adjacencyMatrix = new int[3][3];
  142.         printAdjacency(adjacencyMatrix);
  143.         inputAdjacency(adjacencyMatrix);
  144.         System.out.println("_________________________");
  145.         adjacencyToRib(adjacencyMatrix);
  146.  
  147.         /*
  148.         for(int row = 0; row < adjacencyMatrix.length; row++)
  149.         {
  150.  
  151.             for(int column = 0; column < adjacencyMatrix[row].length; column++)
  152.             {
  153.  
  154.                 System.out.printf("");
  155.  
  156.             }
  157.  
  158.         }
  159.         */
  160.  
  161.  
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement