151 - 矩阵类
Time Limit: 1000 Memory Limit: 65535
Submit: 252 Solved: 137
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
import java.math.BigDecimal;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int r, c;
r = cin.nextInt();
c = cin.nextInt();
Matrix matrix = new Matrix(r,c);
matrix.input(cin);
System.out.println("row:" + matrix.height() + " " + "column:" + matrix.width());
r = cin.nextInt();
c = cin.nextInt();
double v = cin.nextDouble();
matrix.set(r,c,v);
System.out.println("after set value:");
System.out.print(matrix.toString());
r = cin.nextInt();
c = cin.nextInt();
System.out.println("value on (" + r + "," + c + "):"+new BigDecimal(String.valueOf(matrix.get(r,c))).stripTrailingZeros().toPlainString());
r = cin.nextInt();
c = cin.nextInt();
Matrix tmp = new Matrix(r,c);
tmp.input(cin);
System.out.println("after add:");
System.out.print(matrix.add(tmp).toString());
System.out.println("after multiply:");
r = cin.nextInt();
c = cin.nextInt();
tmp = new Matrix(r,c);
tmp.input(cin);
System.out.print(matrix.multiply(tmp));
System.out.println("after transpose:");
System.out.print(matrix.transpose().toString());
}
}
class Matrix {
int rowCnt, colCnt;
double mat[][];
Matrix(int rowCnt, int colCnt) {
this.colCnt = colCnt;
this.rowCnt = rowCnt;
mat = new double[rowCnt][colCnt];
}
public void set(int row, int col, double val) {
row--;
col--;
mat[row][col] = val;
}
public double get(int row, int col) {
row--;
col--;
return mat[row][col];
}
public int width() {
return colCnt;
}
public int height() {
return rowCnt;
}
public Matrix add(Matrix b) {
Matrix ret = new Matrix(this.rowCnt, this.colCnt);
for(int i = 1; i <= rowCnt; i++) {
for(int j = 1; j <= colCnt; j++) {
ret.set(i, j, b.get(i,j)+this.get(i,j));
}
}
return ret;
}
public Matrix multiply(Matrix b) {
Matrix ret = new Matrix(this.rowCnt, b.colCnt);
for(int i = 1; i <= rowCnt; i++) {
for(int j = 1; j <= b.colCnt; j++) {
for(int k = 1; k <= colCnt; k++) {
ret.set(i, j, ret.get(i, j) + this.get(i, k) * b.get(k, j));
}
}
}
return ret;
}
public Matrix transpose() {
Matrix ret = new Matrix(this.colCnt, this.rowCnt);
for(int i = 1; i <= rowCnt; i++) {
for(int j = 1; j <= colCnt; j++) {
ret.set(j, i, this.get(i, j));
}
}
return ret;
}
public void input(Scanner cin) {
for(int i = 1; i <= this.rowCnt; i++) {
for(int j = 1; j <= this.colCnt; j++) {
double val = cin.nextDouble();
this.set(i,j,val);
}
}
}
@Override
public String toString() {
String ret = "";
for(int r = 1; r <= rowCnt; r++) {
for(int c = 1; c <= colCnt - 1; c++) {
ret += (new BigDecimal(String.valueOf(this.get(r,c))).stripTrailingZeros().toPlainString() + " ");
}
ret += new BigDecimal(String.valueOf(this.get(r,colCnt))).stripTrailingZeros().toPlainString() + "\n";
}
return ret;
}
}