当前位置:网站首页>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));
}
}
}
边栏推荐
- 基于宽表的数据建模应用
- [app packaging error] to proceed, either fix the issues identified by lint, or modify your build script as follow
- Node red series (29): use slider and chart nodes to realize double broken line time series diagram
- How to correctly evaluate video image quality
- Node の MongoDB Driver
- Kotlin Compose 与原生 嵌套使用
- 一文读懂TDengine的窗口查询功能
- Flutter development: a way to solve the problem of blank space on the top of listview
- [JS sort according to the attributes in the object array]
- 【C语言】动态内存开辟的使用『malloc』
猜你喜欢
7 月 2 日邀你来TD Hero 线上发布会
为什么不建议你用 MongoDB 这类产品替代时序数据库?
硬核,你见过机器人玩“密室逃脱”吗?(附代码)
The writing speed is increased by dozens of times, and the application of tdengine in tostar intelligent factory solution
卷起來,突破35歲焦慮,動畫演示CPU記錄函數調用過程
从“化学家”到开发者,从甲骨文到 TDengine,我人生的两次重要抉择
Online chain offline integrated chain store e-commerce solution
Three-level distribution is becoming more and more popular. How should businesses choose the appropriate three-level distribution system?
MySQL installation configuration and creation of databases and tables
Getting started with Apache dolphin scheduler (one article is enough)
随机推荐
What should we pay attention to when entering the community e-commerce business?
The most comprehensive promotion strategy: online and offline promotion methods of E-commerce mall
Resolve the horizontal (vertical) sliding conflict between viewpager and WebView
How to empty uploaded attachments with components encapsulated by El upload
oracle 多行数据合并成一行数据
写入速度提升数十倍,TDengine 在拓斯达智能工厂解决方案上的应用
Develop and implement movie recommendation applet based on wechat cloud
Design and exploration of Baidu comment Center
SQL learning alter add new field
Cut off 20% of Imagenet data volume, and the performance of the model will not decline! Meta Stanford et al. Proposed a new method, using knowledge distillation to slim down the data set
如何正确的评测视频画质
百度评论中台的设计与探索
[technical live broadcast] how to rewrite tdengine code from 0 to 1 with vscode
How do enterprises choose the appropriate three-level distribution system?
百度APP 基于Pipeline as Code的持续集成实践
H. 265 introduction to coding principles
Go 语言使用 MySQL 的常见故障分析和应对方法
盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名
一文读懂TDengine的窗口查询功能
观测云与 TDengine 达成深度合作,优化企业上云体验