当前位置:网站首页>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));
}
}
}
边栏推荐
- How to correctly evaluate video image quality
- Roll up, break through 35 year old anxiety, and animate the CPU to record the function call process
- 善用兵者,藏于无形,90 分钟深度讲解最佳推广价值作品
- Single chip microcomputer principle and Interface Technology (esp8266/esp32) machine human draft
- About getfragmentmanager () and getchildfragmentmanager ()
- Node-RED系列(二九):使用slider与chart节点来实现双折线时间序列图
- TDengine可通过数据同步工具 DataX读写
- Kotlin Compose 与原生 嵌套使用
- 基于模板配置的数据可视化平台
- TDengine 离线升级流程
猜你喜欢
Tdengine can read and write through dataX, a data synchronization tool
On July 2, I invite you to TD Hero online press conference
First understanding of structure
H. 265 introduction to coding principles
How to implement complex SQL such as distributed database sub query and join?
(1) Complete the new construction of station in Niagara vykon N4 supervisor 4.8 software
What should we pay attention to when entering the community e-commerce business?
卷起來,突破35歲焦慮,動畫演示CPU記錄函數調用過程
【OpenCV 例程200篇】219. 添加数字水印(盲水印)
一文读懂TDengine的窗口查询功能
随机推荐
Getting started with Apache dolphin scheduler (one article is enough)
如何获取GC(垃圾回收器)的STW(暂停)时间?
What should we pay attention to when entering the community e-commerce business?
[app packaging error] to proceed, either fix the issues identified by lint, or modify your build script as follow
小程序启动性能优化实践
View Slide
如何獲取GC(垃圾回收器)的STW(暫停)時間?
分布式数据库下子查询和 Join 等复杂 SQL 如何实现?
SQL learning - case when then else
Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises
【技术直播】如何用 VSCode 从 0 到 1 改写 TDengine 代码
美图炒币半年亏了3个亿,华为被曝在俄罗斯扩招,AlphaGo的同类又刷爆一种棋,今日更多大新闻在此...
tongweb设置gzip
百度智能小程序巡檢調度方案演進之路
单片机原理与接口技术(ESP8266/ESP32)机器人类草稿
[NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
How to use sqlcipher tool to decrypt encrypted database under Windows system
First understanding of structure
Charm of code language
[listening for an attribute in the array]