daily pastebin goal
30%
SHARE
TWEET

Untitled

a guest Mar 19th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import java.util.Scanner;
  3.  
  4. class Matrix {
  5.  
  6.     int data[][]; // os elementos da matriz em si
  7.     int rows;     // numero de linhas
  8.     int cols;     // numero de colunas
  9.  
  10.     // construtor padrao de matriz
  11.     Matrix(int r, int c) {
  12.         data = new int[r][c];
  13.         rows = r;
  14.         cols = c;
  15.     }
  16.  
  17.     // Ler os rows x cols elementos da matriz
  18.     public void read(Scanner in) {
  19.         for (int i = 0; i < rows; i++) {
  20.             for (int j = 0; j < cols; j++) {
  21.                 data[i][j] = in.nextInt();
  22.             }
  23.         }
  24.     }
  25.  
  26.     // Representacao em String da matrix
  27.     public String toString() {
  28.         String ans = "";
  29.         for (int i = 0; i < rows; i++) {
  30.             for (int j = 0; j < cols; j++) {
  31.                 ans += data[i][j] + " ";
  32.             }
  33.             ans += "\n";
  34.         }
  35.         return ans;
  36.     }
  37.  
  38.     public static Matrix identity(int n) {
  39.         Matrix v;
  40.         v = new Matrix(n, n);
  41.         for (int i = 0; i < n; i++) {
  42.             if (v.rows == v.cols) {
  43.                 v.data[i][i] = 1;
  44.             }
  45.         }
  46.         return v;
  47.     }
  48.  
  49.     public Matrix transpose() {
  50.         Matrix novaNig = new Matrix(this.cols, this.rows);
  51.         for (int i = 0; i < rows; i++) {
  52.             for (int j = 0; j < cols; j++) {
  53.                 novaNig.data[j][i] = this.data[i][j];
  54.             }
  55.         }
  56.         return novaNig;
  57.     }
  58. }
  59.  
  60. class TestMatrix {
  61.  
  62.     public static void main(String[] args) {
  63.         Scanner stdin = new Scanner(System.in);
  64.         Matrix oof = new Matrix(4, 5);
  65.         oof.read(stdin);
  66.         System.out.println(oof);
  67.         System.out.println("----------------------\n\n");
  68.         Matrix oofTranspose = oof.transpose();
  69.         System.out.println(oofTranspose);
  70.     }
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top