用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

151-矩阵类

2018-10-14 作者: 芙蓉妹妹举报

[java]代码库

import java.util.Scanner;
import java.util.Arrays;
public class Main {
	public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);    
 
        int r = scan.nextInt();
        int c = scan.nextInt();
        System.out.println("row:"+r+" column:"+c);
        //输入,修改矩阵的值
        Matrix me = new Matrix(r,c);
        for(int i = 0; i < r; i++)
		{
			for(int j = 0; j < c; j++)
			{
				double a = scan.nextDouble();
				me.set(i, j, a);
			}
		}
        r = scan.nextInt();
        c = scan.nextInt();
        double a = scan.nextDouble();
        me.set(--r, --c, a);
        System.out.println("after set value:");
        me.show();
        //打印具体位置上的值
        r = scan.nextInt();
        c = scan.nextInt();
        System.out.print("value on ("+r+","+c+"):");
        System.out.printf("%.0f\n",me.get(--r, --c));
        //矩阵相加输出
        r = scan.nextInt();
        c = scan.nextInt();
        Matrix jia = new Matrix(r,c);
        for(int i = 0; i < r; i++)
		{
			for(int j = 0; j < c; j++)
			{
			    a = scan.nextDouble();
				jia.set(i, j, a);
			}
		}
        System.out.println("after add:");
        (me.add(jia)).show();
        //矩阵相乘输出
        r = scan.nextInt();
        c = scan.nextInt();
        Matrix cheng = new Matrix(r,c);
        for(int i = 0; i < r; i++)
		{
			for(int j = 0; j < c; j++)
			{
			    a = scan.nextDouble();
				cheng.set(i, j, a);
			}
		}
        System.out.println("after multiply:");
        (me.multiply(cheng)).show();
        //矩阵转置输出
        System.out.println("after transpose:");
        (me.transpose()).show();
        
   }
}
class Matrix
{
	private int row;//行
	private int col;//列
	double[][] m;
	public Matrix(int a, int b)
	{
		row = a;
		col = b;
		m = new double [a][b];
	}
	void set(int r, int c, double a)
	{
		m[r][c] = a;
	}
	double get(int r, int c)
	{
		return m[r][c];
	}
	int width()
	{
		return col;
	}
	int height()
	{
		return row;
	}
	Matrix add(Matrix b)
	{
		Matrix a = new Matrix(row,col);
		for(int i = 0; i < row; i++)
		{
			for(int j = 0; j < col; j++)
				a.set(i,j, b.get(i,j)+this.m[i][j]);
		}
		return a;
	}
	Matrix multiply(Matrix b)
	{
		Matrix a = new Matrix(row,b.width());
		for( int i = 0; i < row; i++)
		{
			for( int j = 0; j < b.width(); j++)
			{
				int sum = 0;
				for( int k = 0; k < col; k++)
					sum += this.get(i,k)*b.get(k, j);
				a.set(i, j, sum);
			}
		}
		return a;
	}
	Matrix transpose()
	{
		Matrix a = new Matrix(col,row);
		for(int i = 0; i < col; i++)
		{
			for(int j = 0; j < row; j++)
				a.set(i,j,this.m[j][i]);
		}
		return a;
	}
	void show()
	{
		for(int i = 0; i < row; i++)
		{
			for(int j = 0; j < col; j++)
			{
				if( j == 0)
					System.out.printf("%.0f",this.get(i, j));
				else 
					System.out.printf(" %.0f",this.get(i, j));
			}
			System.out.println();
		}
	}
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...