gpt4 book ai didi

java - 矩阵中的对角线反转

转载 作者:行者123 更新时间:2023-12-01 13:19:25 24 4
gpt4 key购买 nike

标题说明了一切,我开发了这种对角线方法,用于搜索 Matrix“矩阵”并从中心到最右边的角。我还有另一组从中间到左边的。我现在有一个问题,我该如何使其反转,不是从底部开始,而是实际上从“C”开始,一直到“G”并继续向左移动。

所要做的就是逆转,但我已经尝试了大约2个小时,但仍然无济于事。这实际上是我正在进行的项目的最后一部分,如果有人可以帮助翻转,那就太好了。

这是代码,为了节省空间,我去掉了很大一部分。

public class Word {

public static int curCol = 10;
public static int curRow = 10;

public static String[][] matrix = {{"A","B","C"},
{"D","E","F"},
{"G","H","I"}};

private static void searchDiagonalCenterToRight(String word) {//Center to bottom Righ t. Diagnol Works, debug to go along column is needed

int rowOn = 0;
int colOn = 0;

int resetableCol = curCol;
resetableCol--;//Just resets everything then starts again.

int decreaser = curCol;//What to decrease by everytime it runs 10,9,8,7 all the way to 1
int resetableDec = decreaser;
resetableDec--;

char c;

String toFind = word.toUpperCase();

String developingInverse = "";

int integer = 0;

for(int row = 0; row < curRow; row++)//Matrices Row
{
for(int i = 0; i <= resetableDec; i++)
{

String developingWord = "";

integer = i;

for(int j = integer; j <= resetableDec; j++,integer++)
{
c = matrix[j][integer+row].charAt(0);//Sets to whatever letter it is on
char uC = Character.toUpperCase(c);
developingWord = developingWord + "" +uC;

System.out.println("On Row: " + row + " Started From: " + integer + " Now On: " + j);
System.out.println("Processing Letter: " + matrix[j][integer] + " Adding Letter To: " + developingWord);

}

}
resetableDec--;
}
System.out.println("\nNo Matching Word Was Found, Center To Left.");
}

}

最佳答案

这是代码

public class ReverseDiagonalMatrix {

public static void main(String[] args) {
int [][] a = {{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9,10,11,12},
{13,14,15,16}};
int a1[][]= {{1,2,3},
{4,5,6},
{7,8,9}};
int a2[][]= {{1,2},
{3,4}};
int [][] a3 = {{ 1, 2, 3, 4, 5},
{ 6, 7, 8, 9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25}};
System.out.println("==========5x5==============");
findReverseDiagonalOrder(a3);
System.out.println("==========4x4==============");
findReverseDiagonalOrder(a);
System.out.println("===========3x3=============");
findReverseDiagonalOrder(a1);
System.out.println("===========2x2=============");
findReverseDiagonalOrder(a2);
}

public static void findReverseDiagonalOrder(int[][] a) {
int m = a.length;

int row=0;
int col = m-1;

for(int i=0;i<m*m;i++) {
System.out.println(a[row][col]);
if(col==m-1) {
if(row==0)
col--;
else {
col= (row==col)? 0:col-(row+1);
row= (row==m-1)? 1:0;
}
}
else if(row==m-1) {
if(col-1==0 && col>0)
col--;
else {
row = m-col;
col=0;
}

}
else {
row++;
col++;
}
}
}
}

关于java - 矩阵中的对角线反转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22161684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com