151 - 矩阵类
Time Limit: 1000 Memory Limit: 65535
Submit: 485 Solved: 274
Description
利用二维数组(double[])实现一个矩阵类:Matrix。要求提供以下方法:(1)set(int row, int col, double value):将第row行第col列的元素赋值为value;(2)get(int row,int col):取第row行第col列的元素;(3)width():返回矩阵的列数;(4)height():返回矩阵的行数;(5)Matrix add(Matrix b):返回当前矩阵与矩阵b相加后的矩阵;(6)Matrix multiply(Matrix b):返回当前矩阵与矩阵b相乘后的矩阵。(7)Matrix transpose():返回当前矩阵的转置矩阵;(8)toString():以行和列的形式打印出当前矩阵。
Input
矩阵的行列数
矩阵的数据
设置矩阵值的行、列和值
获取矩阵值的行、列
待相加矩阵的行列数
待相加矩阵的值
待相乘矩阵的行列数
待相乘矩阵的值
Output
矩阵的行、列数
设置矩阵值后的矩阵
某行某列的矩阵值
矩阵相加结果
矩阵相乘结果
矩阵转置结果
Sample Input
3 3
1 2 3
4 5 6
7 8 9
2 3 8
1 3
3 3
1 2 3
4 5 6
7 8 9
3 2
1 2
1 2
1 2
Sample Output
row:3 column:3
after set value:
1 2 3
4 5 8
7 8 9
value on (1,3):3
after add:
2 4 6
8 10 14
14 16 18
after multiply:
6 12
17 34
24 48
after transpose:
1 4 7
2 5 8
3 8 9
HINT
Pre Append Code
Post Append Code
import java.util.Scanner;
class Matrix {
double data[][];
int row,col;
//构造
public Matrix(int row,int col) {
this.row=row;
this.col=col;
data=new double[row][col];
}
//赋值
public void setMatrix(int row, int col, double value){
data[row][col]=value;
}
//取值
public double getMatrix(int row,int col) {
return data[row][col];
}
public int width()
{
return col;
}
public int height()
{
return row;
}
public Matrix add(Matrix b) {
for (int i = 0; i < b.height(); i++) {
for (int j = 0; j < b.width(); j++) {
double temp = 0;
temp += this.getMatrix(i,j) + b.getMatrix(i,j);
this.setMatrix(i,j,temp);
}
}
return this;
}
public Matrix multiply(Matrix b){
Matrix c = new Matrix(this.height(),b.width());
for (int i = 0; i < this.height(); i++) {
for (int j = 0; j < b.width(); j++) {
double temp = 0;
for (int k = 0; k < this.height(); k++) {
temp += this.getMatrix(i,k) * b.getMatrix(k,j);
}
c.setMatrix(i,j,temp);
}
}
return c;
}
public Matrix transpose(){//转置矩阵
for (int i = 0; i < this.height(); i++) {
for (int j = 0; j < i; j++) {
double temp = this.getMatrix(i,j);
this.setMatrix(i,j,this.getMatrix(j,i));
this.setMatrix(j,i,temp);
}
}
return this;
}
public String toString() {//以行和列的形式打印出当前矩阵
String s = "";
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if(j != 0)
s += " ";
s += (int)this.getMatrix(i,j);
}
if(i != row - 1)
s += "\n";
}
return s;
}
}
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int row = scan.nextInt();
int col = scan.nextInt();
System.out.println("row:" + row + " column:" + col);
Matrix matrix = new Matrix(row,col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
double t = scan.nextDouble();
matrix.setMatrix(i,j,t);
}
}
//设置矩阵值后的矩阵
System.out.println("after set value:");
int m = scan.nextInt();
int n = scan.nextInt();
double p = scan.nextDouble();
matrix.setMatrix(m - 1,n - 1,p);
System.out.println(matrix);
//某行某列的矩阵值
m = scan.nextInt();
n = scan.nextInt();
System.out.println("value on (" + m + "," + n + "):" + (int)matrix.getMatrix(m - 1,n - 1));
//矩阵相加结果
System.out.println("after add:");
m = scan.nextInt();
n = scan.nextInt();
Matrix matrix2 = new Matrix(m,n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
double t = scan.nextDouble();
matrix2.setMatrix(i,j,t);
}
}
System.out.println(matrix2.add(matrix));
//矩阵相乘结果
System.out.println("after multiply:");
m = scan.nextInt();
n = scan.nextInt();
Matrix matrix3 = new Matrix(m,n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
double t = scan.nextDouble();
matrix3.setMatrix(i,j,t);
}
}
Matrix matrix1 = matrix;
System.out.println(matrix.multiply(matrix3));
//矩阵转置结果
System.out.println("after transpose:");
System.out.println(matrix1.transpose());
scan.close();
}
}