posts - 495,  comments - 11,  trackbacks - 0

          //轉(zhuǎn)載請注明出處:http://hi.baidu.com/jadmin/blog/item/163ed2f8a8e88b0dd8f9fd46.html
          /**
          * @(#)MatrixMultiply.java
          *
          *
          * @author JAdmin
          * @version 1.00 2007/8/21
          */

          import java.util.Scanner;

          public class MatrixMultiply
          {
          public static void main(String[] args)
          {
          ??? int m,n,p;
          ??? System.out.println("請輸入3個矩陣參數(shù)m,n,p.示例a[m][n]*b[n][p]=c[m][p]");
          ??? Scanner sr = new Scanner(System.in);
          ??? System.out.print("m=");
          ??? m = sr.nextInt();
          ??? System.out.print("n=");
          ??? n = sr.nextInt();
          ??? System.out.print("p=");
          ??? p = sr.nextInt();

          ??? int[][] a = new int[m][n];
          ??? int[][] b = new int[n][p];
          ??? int[][] c = new int[m][p];

          ??? System.out.println("請輸入數(shù)組a[" + m + "][" + n + "]");
          ??? input(a);
          ??? System.out.println("請輸入數(shù)組b[" + n + "][" + p + "]");
          ??? input(b);

          ??? //以下開始計算c[][]
          ??? for(int i = 0;i < c.length;i++)
          ???? for(int j = 0;j < c[0].length;j++)
          ????? c[i][j] = getMultiItem(a,b,i,j);

          ??? System.out.println("************The matrix a[][]************");
          ??? display(a);
          ??? System.out.println("************The matrix b[][]************");
          ??? display(b);
          ??? System.out.println("************The matrix c[][]************");
          ??? display(c);
          }

          private static void input(int[][] arr)
          {
          ??? Scanner sr = new Scanner(System.in);
          ??? for(int i = 0;i < arr.length;i++)
          ???? for(int j = 0;j < arr[0].length;j++)
          ?????? arr[i][j] = sr.nextInt();
          }

          private static void display(int[][] arr)
          {
          ??? for(int i = 0;i < arr.length;i++)
          ??? {
          ???? for(int j = 0;j < arr[0].length;j++)
          ????? System.out.print(arr[i][j]+" ");
          ???? System.out.println();
          ??? }
          }

          private static int getMultiItem(int[][] a,int[][] b,int p,int q)
          {
          ??? //計算元素c[p][q]
          ??? int tmp = 0;

          ??? for(int i = 0;i < a[0].length;i++)
          ???? tmp += a[p][i]*b[i][q];
          ??? return tmp;
          }
          }

          posted on 2007-08-21 18:38 jadmin 閱讀(64) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 平阴县| 台湾省| 攀枝花市| 法库县| 平定县| 鹿邑县| 桐梓县| 博爱县| 巴彦淖尔市| 鄯善县| 新昌县| 平昌县| 嘉祥县| 图片| 维西| 朝阳市| 洪泽县| 临沭县| 钦州市| 会宁县| 大安市| 逊克县| 石台县| 克拉玛依市| 北宁市| 博爱县| 文安县| 华坪县| 顺昌县| 贺兰县| 聂拉木县| 翼城县| 邹城市| 航空| 桂林市| 涟水县| 炎陵县| 汽车| 张家口市| 五大连池市| 宜黄县|