1. 程式人生 > >java試題,兩個陣列相乘

java試題,兩個陣列相乘

圖片
程式碼如下:

package erweishuzu;

public class shuzuxiangcheng {

	public static void main(String[] args) {

         int[][] a = new int[][]{{2,3,4},{4,6,8}};
         int[][] b = new int[][]{{1,5,2,8},{5,9,10,-3},{2,7,-5,-18}};
         int[][] c = new int[2][4];
         int sum = 0;
         for(int i=0;i<c.length;i++)
         {
        	 for(int j=0;j<c[i].length;j++)
        	 {
        		 sum = 0;
        		 for(int k=0;k<a[i].length;k++)
        		 {
        			 sum +=a[i][k] * b[k][j];
        			 
        		 }
        		 c[i][j] = sum;
        		 
        		 System.out.print(c[i][j] +" ");
        	 }
        	 System.out.println(" ");
         }
	
	}

}