主类
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import java.util.ArrayList; |
| import java.util.TreeMap; |
| |
| |
| |
| |
| |
| |
| |
| |
| public class main { |
| |
| |
| public static void main(String[] args) { |
| |
| InputMatrix inputMatrix = new InputMatrix(); |
| |
| SparseMatrix sparseMatrix1 = inputMatrix.getSparseMatrix(); |
| |
| translate(sparseMatrix1).print(); |
| |
| } |
| |
| static SparseMatrix translate(SparseMatrix sparseMatrix1) { |
| |
| |
| |
| |
| |
| |
| SparseMatrix sparseMatrix2 = new SparseMatrix(); |
| setProfile(sparseMatrix1, sparseMatrix2); |
| |
| |
| |
| int[] elementNumOfEachRow2 = new int[sparseMatrix1.ncol]; |
| |
| for (int i = 0; i < sparseMatrix1.ncol; i++) { |
| elementNumOfEachRow2[i] = 0; |
| } |
| |
| for (int i = 0; i < sparseMatrix1.num; i++) { |
| |
| |
| elementNumOfEachRow2[sparseMatrix1.getTriple(i).col - 1]++; |
| System.out.println(sparseMatrix1.getTriple(i)); |
| } |
| |
| int[] startInsertArray = divideZone(sparseMatrix1, elementNumOfEachRow2); |
| getResortTripleList(startInsertArray, sparseMatrix1, sparseMatrix2); |
| |
| return sparseMatrix2; |
| |
| } |
| |
| static void setProfile(SparseMatrix sparseMatrix1, SparseMatrix sparseMatrix2) { |
| sparseMatrix2.ncol = sparseMatrix1.nrow; |
| sparseMatrix2.nrow = sparseMatrix1.ncol; |
| sparseMatrix2.num = sparseMatrix1.num; |
| } |
| |
| |
| static int[] divideZone(SparseMatrix sparseMatrix, int[] elementNumOfEachRow2) { |
| |
| int[] startInsertArray = new int[sparseMatrix.ncol]; |
| startInsertArray[0] = 0; |
| for (int i = 1; |
| i < sparseMatrix.ncol; i++) { |
| startInsertArray[i] = startInsertArray[i - 1] + elementNumOfEachRow2[i - 1]; |
| } |
| |
| return startInsertArray; |
| } |
| |
| static void getResortTripleList(int[] startInsertArray, SparseMatrix sparseMatrix1, SparseMatrix sparseMatrix2) { |
| |
| int position; |
| Triple elementPoint, srcElementPoint; |
| |
| for (int j = 0; j < sparseMatrix2.num; j++) { |
| sparseMatrix2.tripleList.add(new Triple()); |
| } |
| for (int i = 0; i < sparseMatrix1.num; i++) { |
| |
| position = getPosition(i, startInsertArray, sparseMatrix1); |
| |
| System.out.println(position); |
| |
| elementPoint = sparseMatrix2.getTriple(position); |
| |
| srcElementPoint = sparseMatrix1.getTriple(i); |
| |
| elementPoint.setRow(srcElementPoint.getCol()); |
| elementPoint.setCol(srcElementPoint.getRow()); |
| elementPoint.setData(srcElementPoint.getData()); |
| |
| updateStartInsertArray(i, startInsertArray, sparseMatrix1); |
| } |
| |
| } |
| |
| |
| |
| |
| static int getPosition(int index, int[] startInsertArray, SparseMatrix sparseMatrix1) { |
| return startInsertArray[sparseMatrix1.getTriple(index).getCol()-1]; |
| |
| } |
| |
| static void updateStartInsertArray(int index, int[] startInsertArray, SparseMatrix sparseMatrix1) { |
| startInsertArray[sparseMatrix1.tripleList.get(index).col-1]++; |
| } |
| |
| } |
| |
| class Triple { |
| |
| |
| |
| int row; |
| int col; |
| int data; |
| |
| @Override |
| |
| public String toString() { |
| return "Triple{" + |
| "row=" + row + |
| ", col=" + col + |
| ", data=" + data + |
| '}'; |
| |
| } |
| |
| |
| public int getRow() { |
| return this.row; |
| } |
| |
| public void setRow(int row) { |
| this.row = row; |
| } |
| |
| public int getCol() { |
| return this.col; |
| } |
| |
| public void setCol(int col) { |
| this.col = col; |
| } |
| |
| public int getData() { |
| return this.data; |
| } |
| |
| public void setData(int data) { |
| this.data = data; |
| } |
| |
| } |
| |
| |
| class SparseMatrix { |
| public void setTripleList(ArrayList<Triple> tripleList) { |
| this.tripleList = tripleList; |
| } |
| |
| public void setNrow(int nrow) { |
| this.nrow = nrow; |
| } |
| |
| public void setNcol(int ncol) { |
| this.ncol = ncol; |
| } |
| |
| public void setNum(int num) { |
| this.num = num; |
| } |
| |
| ArrayList<Triple> tripleList = new ArrayList<>(); |
| int nrow, |
| ncol, |
| num; |
| |
| Triple getTriple(int index) { |
| return tripleList.get(index); |
| } |
| |
| void print() { |
| int[][] matrix = new int[nrow][ncol]; |
| |
| |
| for (int i = 0; i < num; i++) { |
| Triple triple = getTriple(i); |
| if (triple.getData() != 0) { |
| matrix[triple.getRow()-1][triple.getCol()-1] = triple.getData(); |
| } |
| } |
| |
| for (int i = 0; i < nrow; i++) { |
| for (int j = 0; j < ncol; j++) { |
| System.out.print(matrix[i][j]+" "); |
| } |
| System.out.println(); |
| } |
| } |
| } |
| |
辅助功能类(读入任意规格的矩阵)
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import java.io.Reader; |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.List; |
| import java.util.Scanner; |
| |
| public class InputMatrix { |
| |
| |
| |
| public static int[][] ReadMatrix() { |
| |
| Scanner sc = new Scanner(System.in); |
| |
| List<List<String>> lists = new ArrayList<>(); |
| |
| String Line = null; |
| |
| while ((Line = sc.nextLine()).length() != 0) { |
| |
| |
| |
| lists.add(Arrays.asList(Line.trim().split(" "))); |
| |
| |
| |
| |
| |
| } |
| |
| sc.close(); |
| |
| int m = lists.size(); |
| int n = lists.get(0).size(); |
| int[][] matrix = new int[m][n]; |
| |
| for (int i = 0; i < m; i++) { |
| for (int j = 0; j < n; j++) { |
| matrix[i][j] = Integer.parseInt(lists.get(i).get(j)); |
| } |
| } |
| return matrix; |
| } |
| |
| |
| public SparseMatrix getSparseMatrix() { |
| int[][] matrix = ReadMatrix(); |
| int nrow = matrix.length; |
| int ncol = matrix[0].length; |
| SparseMatrix sparseMatrix = new SparseMatrix(); |
| int count = 0; |
| |
| for (int i = 0; i < nrow; i++) { |
| for (int j = 0; j < ncol; j++) { |
| if (matrix[i][j] != 0) { |
| count++; |
| Triple triple = new Triple(); |
| |
| triple.setRow(i+1); |
| triple.setCol(j+1); |
| triple.setData(matrix[i][j]); |
| sparseMatrix.tripleList.add(triple); |
| } |
| } |
| } |
| sparseMatrix.setNum(count); |
| sparseMatrix.setNrow(nrow); |
| sparseMatrix.setNcol(ncol); |
| return sparseMatrix; |
| } |
| |
| |
| public static void main(String[] args) { |
| |
| System.out.println("test:..."); |
| |
| |
| |
| |
| |
| |
| InputMatrix inputMatrix=new InputMatrix(); |
| inputMatrix.getSparseMatrix().print(); |
| |
| } |
| } |
| |
编写和调试心得
我们应该明确的是,尽管调试很重要,但是不应当轻易,总是依赖于调试,容易造成编码的马虎,毕竟调试(这是很花时间的)
我们编码时一定要尽可能地准确,对于自己定义的任意变量或对象,乃至每一个函数和方法,都必须要有十分清晰的目的,容不得半点含糊,文档注释一定要跟上,这对于算法思路的检查乃至调试时都提供了遍历
这其中尤为重要的是同一度量体系(如果无法全局同一,也一定按模块同一,而且附加必要的计量系统/参考系说明)
比如二位数组/矩阵相关的计算问题中,单个元素的坐标从1计数,而放到二位数组中,下表从0开始计数,两个地方不要混淆.
应当养成描述清楚需求,再编写对应的函数的习惯
编写方法/函数
如果方法的用途比较复杂,不宜完全体现在方法名上
可以考虑简写,不过要记得把函数的用法文档写清楚
如果只是写小程序,则不要过多拘泥于将功能外包
可以通过代码功能分块(功能任务分配通过文档注释的方式来体现逻辑性
同样的,在编写一个大的方法也可以这么做
通过创建变量保存比较长的表达式,来减少重复计算,也可以是代码更加简洁直观,高效
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了