lamiastella

microsoft_Margaret

Sep 30th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.23 KB | None | 0 0
  1. package microsoft_competition;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class MatrixMultiplication {
  7.     private int a[][];
  8.     private int b[][];
  9.     private int sum[][];
  10.  
  11.     public void calculateSum(int a[][], int b[][]) {
  12.         sum = new int[a.length][b[0].length];
  13.  
  14.         // Iterate through rows of a
  15.         for (int i = 0; i < a.length; i++) {
  16.            
  17.             // In the current row of A, loop through each column value
  18.             for (int j=0; j<b[0].length; j++){
  19.                
  20.                 for(int k=0; k< a[0].length; k++){
  21.                     sum[i][j] += a[i][k]*b[k][j];
  22.                 }
  23.             }
  24.  
  25.         }
  26.     }
  27.     public void ReadMatrix(String s){
  28.         Scanner sc= new Scanner(s).useDelimiter("/n");
  29.  
  30.         ArrayList<ArrayList<Integer>> aValues = new ArrayList<ArrayList<Integer>>();
  31.         ArrayList<ArrayList<Integer>> bValues = new ArrayList<ArrayList<Integer>>();
  32.         aValues.clear();
  33.         bValues.clear();
  34.  
  35.         while(sc.hasNext()) {
  36.             String currentLine = sc.nextLine();
  37.  
  38.             // We have one line with one or more matrix rows
  39.             // e.g. |1 2 3| |3 4|
  40.  
  41.             ArrayList<Integer> currentAValues = new ArrayList<Integer>();
  42.             ArrayList<Integer> currentBValues = new ArrayList<Integer>();
  43.  
  44.             currentAValues.clear();
  45.             currentBValues.clear();
  46.  
  47.             // See if we should add values to A or B
  48.             //If A: line starts with "|"
  49.             //If B: line starts with " "
  50.  
  51.             if (currentLine.charAt(0) == '|') {
  52.                 // Add values to A
  53.  
  54.                 Boolean bStarted = false;
  55.                 int indexWhereBStarted = -1;
  56.                 String currentNum = "";
  57.                
  58.                 for (int i = 1; i < currentLine.length(); i++) {
  59.  
  60.                     if (currentLine.charAt(i) != '|' && !bStarted) {
  61.                         // Add to A
  62.                        
  63.                         if ((currentLine.charAt(i) != ' ')) {
  64.                            
  65.                             String currentChar = String.valueOf(currentLine.charAt(i));
  66.                             currentNum += currentChar;
  67.                             // currentAValues.add(Integer.parseInt(currentChar));
  68.                         }
  69.                         else {
  70.                             // we have found a space
  71.                             // currentNum has all digits
  72.                             if (currentNum != "") {
  73.                                 currentAValues.add(Integer.parseInt(currentNum));
  74.                                 currentNum = "";
  75.                             }
  76.                         }
  77.                     }
  78.                     else {
  79.                         if (bStarted == false) {
  80.                             // Start adding to B
  81.                             bStarted = true;
  82.                             indexWhereBStarted = i;
  83.                         }
  84.  
  85.                         // if the next char is also '|', then b started
  86.                         if (currentLine.charAt(indexWhereBStarted+2) != '|') {
  87.                             break;
  88.                         }
  89.                         else if (currentLine.charAt(i) != ' ' && currentLine.charAt(i) != '|') {
  90.                             // add values to b
  91.                             String currentChar = String.valueOf(currentLine.charAt(i));
  92.                             currentBValues.add(Integer.parseInt(currentChar));
  93.                         }
  94.                     }
  95.                 }
  96.             } // end check if A values present
  97.  
  98.             // In this case, only B values are present, e.g. " | 4 5 5 |"
  99.             else if (currentLine.charAt(0) == ' ') {
  100.                 // Add values to B
  101.  
  102.                 for (int i = 1; i < currentLine.length(); i++) {
  103.                     if (currentLine.charAt(i) != '|' && (currentLine.charAt(i) != ' ')) {
  104.                         // Add to B
  105.                         String currentChar = String.valueOf(currentLine.charAt(i));
  106.                         currentBValues.add(Integer.parseInt(currentChar));
  107.                     }
  108.                 }
  109.             }
  110.  
  111.             if (currentAValues != null && !currentAValues.isEmpty()) {
  112.                 aValues.add(currentAValues);
  113.             }
  114.  
  115.             if (currentBValues != null && !currentBValues.isEmpty()) {
  116.                 bValues.add(currentBValues);
  117.             }
  118.  
  119.         } // end for loop that processes entire line
  120.  
  121.         // At this point, we have 2d arrays in nested lists
  122.         // need to translate them to 2d arrays
  123.         a=new int[aValues.size()][aValues.get(0).size()];
  124.         b=new int[bValues.size()][bValues.get(0).size()];
  125.  
  126.         for (int i=0; i< a.length;i++) {
  127.             if (aValues.size() > 0) {
  128.                 for (int j=0; j<a[i].length; j++){
  129.                     a[i][j]=aValues.get(i).get(j);
  130.                 }
  131.             }
  132.         }
  133.  
  134.         for (int i=0; i<b.length;i++) {
  135.             if (bValues.size() > 0) {
  136.                 for (int j=0; j<b[i].length; j++){
  137.                     b[i][j]=bValues.get(i).get(j);
  138.                 }
  139.             }
  140.         }
  141.     }
  142.  
  143.     public static void main(String argc[]){
  144.         MatrixMultiplication mm= new MatrixMultiplication();
  145.         mm.ReadMatrix("| 10 2 3 | |  7  8 |\n"
  146.                 + "| 4 5 6 | |  9 10 |\n"
  147.           + " | 11 12 |");
  148.         mm.calculateSum(mm.a, mm.b);
  149.  
  150.         for (int i = 0; i < mm.sum.length; i++) {
  151.             System.out.print("|");
  152.             for (int j = 0; j < mm.sum[i].length; j++) {
  153.                 System.out.print(mm.sum[i][j] + " ");
  154.             }
  155.             System.out.print("|\n");
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment