当前位置:网站首页>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));
}
}
}
边栏推荐
- Common fault analysis and Countermeasures of using MySQL in go language
- What about wechat mall? 5 tips to clear your mind
- Openes version query
- The writing speed is increased by dozens of times, and the application of tdengine in tostar intelligent factory solution
- MySQL installation configuration and creation of databases and tables
- 剪掉ImageNet 20%数据量,模型性能不下降!Meta斯坦福等提出新方法,用知识蒸馏给数据集瘦身...
- Flutter development: use safearea
- Viewpager pageradapter notifydatasetchanged invalid problem
- mysql80服务不启动
- Why don't you recommend using products like mongodb to replace time series databases?
猜你喜欢
Wechat applet - simple diet recommendation (3)
The popularity of B2B2C continues to rise. What are the benefits of enterprises doing multi-user mall system?
Is it really reliable for AI to make complex decisions for enterprises? Participate in the live broadcast, Dr. Stanford to share his choice | qubit · viewpoint
一文读懂TDengine的窗口查询功能
Cross process communication Aidl
善用兵者,藏于无形,90 分钟深度讲解最佳推广价值作品
【技术直播】如何用 VSCode 从 0 到 1 改写 TDengine 代码
The most comprehensive promotion strategy: online and offline promotion methods of E-commerce mall
Charm of code language
Idea debugs com intellij. rt.debugger. agent. Captureagent, which makes debugging impossible
随机推荐
Kotlin Compose 多个条目滚动
Baidu app's continuous integration practice based on pipeline as code
tongweb设置gzip
ThreadLocal source code learning
Roll up, break through 35 year old anxiety, and animate the CPU to record the function call process
How to implement complex SQL such as distributed database sub query and join?
On July 2, I invite you to TD Hero online press conference
如何獲取GC(垃圾回收器)的STW(暫停)時間?
卷起来,突破35岁焦虑,动画演示CPU记录函数调用过程
【OpenCV 例程200篇】219. 添加数字水印(盲水印)
Kotlin compose multiple item scrolling
The essence of persuasion is to remove obstacles
Why do offline stores need cashier software?
Are databases more popular as they get older?
Apache DolphinScheduler 系统架构设计
分布式数据库下子查询和 Join 等复杂 SQL 如何实现?
QT timer realizes dynamic display of pictures
【js 根据对象数组中的属性进行排序】
Community group buying has triggered heated discussion. How does this model work?
从“化学家”到开发者,从甲骨文到TDengine,我人生的两次重要抉择