当前位置:网站首页>Matrix processing practice
Matrix processing practice
2022-07-05 10:00:00 【Sunqk5665】
Using two-dimensional arrays (int[]) Implement a matrix class :Matrix.
The following methods are required :
(1)set (int row, int col, int value) Will be the first row Xing di col The element of the column is assigned to value;
(2)get(int row,int col) : Take the first place row Xing di col The elements of the column ;
(3)width( ): Returns the number of columns in the matrix ;
(4)height( ): Returns the number of rows in the matrix ;
(5)Matrix add ( Matrix b) : Returns the current matrix and the matrix b The added matrix ;
(6)Matrix multiply ( Matrix b) : Returns the current matrix and the matrix b Matrix after multiplication .
(7)Matrix transpose(): Returns the transpose matrix of the current matrix ;
(8)print(): Print out the current matrix in rows and columns .
package PTAworks;
/** * @ClassName: aaa * @Description: TODO * @Author: Sunqker * @Date: 2021/10/13 15:37 */
import java.util.Scanner;
public class aaa {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
// Get the number of rows and columns of the matrix
Matrix matrix = new Matrix(cin.nextInt(), cin.nextInt());
Matrix t; // Temporary storage matrix
// Matrix data
matrix.input(cin); // Enter matrix information
System.out.println("row:" + matrix.height() + " " + "column:" + matrix.width());
// Sets the row of the matrix value 、 Columns and values
matrix.set(cin.nextInt(), cin.nextInt(), cin.nextInt());
System.out.println("after set value:");
matrix.print();
// Get the row of the matrix value 、 Column
int r = cin.nextInt();
int c = cin.nextInt();
System.out.println("value on (" + r + "," + c + "):" + matrix.get(r,c));
// Get the number of rows and columns of the matrix to be added
Matrix tmp = new Matrix(cin.nextInt(),cin.nextInt());
// Get the value of the matrix to be added
tmp.input(cin);
System.out.println("after add:");
t = matrix.add(tmp); // Matrix addition
t.print(); // Print the added matrix
// The number of rows and columns of the matrix to be multiplied
tmp = new Matrix(cin.nextInt(),cin.nextInt());
Get the value of the matrix to be added
tmp.input(cin);
System.out.println("after multiply:");
t = matrix.multiply(tmp);// matrix multiplication
// The value of the matrix to be multiplied
t.print(); // Print the multiplication result
System.out.println("after transpose:");
t = matrix.transpose();
t.print();
}
}
class Matrix{
private int row, col;
private int[][] d;
public Matrix(int row, int col) {
this.row = row;
this.col = col;
d = new int[row][col];
}
public void input(Scanner cin) {
for(int i = 1; i <= this.row; i++) {
for(int j = 1; j <= this.col; j++) {
int val = cin.nextInt();
this.set(i,j,val);
}
}
}
// 1 Will be the first row Xing di col The element of the column is assigned to value
public void set(int row, int col, int value){
d[--row][--col] = value;
}
// 2 Take the first place row Xing di col The elements of the column
public int get( int row, int col ){
return d[--row][--col];
}
// 3 Returns the number of columns in the matrix
public int width(){
return col;
}
// 4 Returns the number of rows in the matrix
public int height(){
return row;
}
// 5 Returns the current matrix and the matrix b The added matrix
public Matrix add( Matrix b){
Matrix t = new Matrix(this.row, this.col);
for(int i = 1; i <= this.row; ++ i){
for(int j = 1; j <= this.col; ++ j ){
t.set(i, j, this.get(i, j) + b.get(i, j));
}
}
return t;
}
// 6 Returns the current matrix and the matrix b Matrix after multiplication
public Matrix multiply( Matrix b){
Matrix t = new Matrix(this.row, b.width());
for(int i = 1; i <= this.row; ++ i){
for(int j = 1; j <= b.width(); ++ j ){
int tt = 0;
for(int k = 1; k <= this.col; k ++ ){
tt += this.get(i, k) * b.get(k, j);
}
t.set(i, j, tt);
}
}
return t;
}
// 7 Returns the transpose matrix of the current matrix
public Matrix transpose(){
Matrix t = new Matrix(this.row, this.col);
for( int i = 1; i <= this.row; i ++ ){
for ( int j = 1; j <= this.col; j ++ ){
t.set(j, i , this.get(i, j));
}
}
return t;
}
// 8 Row and column print matrix
public void print(){
//DecimalFormat d = new DecimalFormat("#"); // Control output format , No decimal
for(int i = 1; i <= this.row; ++ i){
for( int j = 1; j <= this.col - 1; ++ j ){
System.out.print(this.get(i, j) + " ");
}
System.out.println(this.get(i, col));
}
}
}
边栏推荐
- Meitu lost 300 million yuan in currency speculation for half a year. Huawei was exposed to expand its enrollment in Russia. Alphago's peers have made another breakthrough in chess. Today, more big new
- Wechat applet - simple diet recommendation (3)
- The writing speed is increased by dozens of times, and the application of tdengine in tostar intelligent factory solution
- Understand the window query function of tdengine in one article
- Kotlin compose multiple item scrolling
- [NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
- 基于宽表的数据建模应用
- What should we pay attention to when entering the community e-commerce business?
- 从“化学家”到开发者,从甲骨文到TDengine,我人生的两次重要抉择
- About getfragmentmanager () and getchildfragmentmanager ()
猜你喜欢
【数组的中的某个属性的监听】
From "chemist" to developer, from Oracle to tdengine, two important choices in my life
Kotlin compose and native nesting
揭秘百度智能测试在测试自动执行领域实践
百度评论中台的设计与探索
TDengine 已经支持工业英特尔 边缘洞见软件包
On July 2, I invite you to TD Hero online press conference
Small program startup performance optimization practice
How to implement complex SQL such as distributed database sub query and join?
基于宽表的数据建模应用
随机推荐
Small program startup performance optimization practice
Apache DolphinScheduler 入门(一篇就够了)
[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
What should we pay attention to when entering the community e-commerce business?
Charm of code language
ThreadLocal source code learning
Kotlin compose multiple item scrolling
tongweb设置gzip
Why don't you recommend using products like mongodb to replace time series databases?
Node red series (29): use slider and chart nodes to realize double broken line time series diagram
TDengine可通过数据同步工具 DataX读写
QT realizes signal transmission and reception between two windows
Windows uses commands to run kotlin
搞数据库是不是越老越吃香?
Go 语言使用 MySQL 的常见故障分析和应对方法
Community group buying exploded overnight. How should this new model of e-commerce operate?
Cent7 Oracle database installation error
H.265编码原理入门
Cross process communication Aidl
Dry goods sorting! How about the development trend of ERP in the manufacturing industry? It's enough to read this article