从右向左扫描Java中的二维对角数组?
我的代码实际上是从左到右扫描阵列:从右向左扫描Java中的二维对角数组?,java,Java,我的代码实际上是从左到右扫描阵列: package diagonals; public class Diagonal { public static void main(String[] args) { int[][] matrix = {{20, 19, 18, 17, 16}, {30, 31, 32, 33, 34}, {44, 45, 46, 47, 48},
package diagonals;
public class Diagonal {
public static void main(String[] args) {
int[][] matrix = {{20, 19, 18, 17, 16},
{30, 31, 32, 33, 34},
{44, 45, 46, 47, 48},
{74, 55, 76, 77, 78},
{61, 62, 63, 64, 65}
};
int width = 5;
int height = 5;
int startRow = height - 1;
int startColumn = 0;
int column = 0;
do {
int row = startRow;
column = startColumn;
do {
System.out.print(matrix[row][column] + " ");
++row;
++column;
} while ((row < height) && (column < width));
System.out.println("");
if (startRow > 0) {
--startRow;
} else {
++startColumn;
}
} while (startColumn < width);
}
}
如何更改此代码以从按钮左上方扫描阵列
我的预期输出:
65
78 64
48 77 63
34 47 76 62
16 33 46 55 61
17 32 45 74
18 31 44
19 30
20
我尝试将startColumn更改为height-1,但它只输出65,这是扫描的第一个数字。publicstaticvoidmain(String[]args){
public static void main(String []args){
int[][] matrix = {{20, 19, 18, 17, 16},
{30, 31, 32, 33, 34},
{44, 45, 46, 47, 48},
{74, 55, 76, 77, 78},
{61, 62, 63, 64, 65}
} ;
int width = 5;
int height = 5;
int startRow = height - 1;
int startColumn = width - 1;
int column = 0;
do {
int row = startRow;
column = startColumn;
do {
System.out.print(matrix[row][column] + " ");
++row;
--column;
} while ((row < height) && (column > -1));
System.out.println("");
if (startRow > 0) {
--startRow;
} else {
--startColumn;
}
} while (startColumn > -1);
}
int[]矩阵={{20,19,18,17,16},
{30, 31, 32, 33, 34},
{44, 45, 46, 47, 48},
{74, 55, 76, 77, 78},
{61, 62, 63, 64, 65}
} ;
整数宽度=5;
整数高度=5;
int startRow=高度-1;
int startColumn=宽度-1;
int列=0;
做{
int row=startRow;
列=起始列;
做{
系统输出打印(矩阵[行][列]+“”);
++行;
--栏目;
}而((行<高度)和&(列>-1));
System.out.println(“”);
如果(startRow>0){
--斯塔特罗;
}否则{
--星柱;
}
}while(startColumn>-1);
}
如果我正确理解了你的问题陈述,你可以试试这个
class Diagonal {
public static void main (String[] args) throws java.lang.Exception {
int[][] matrix = {{20, 19, 18, 17, 16},
{30, 31, 32, 33, 34},
{44, 45, 46, 47, 48},
{74, 55, 76, 77, 78},
{61, 62, 63, 64, 65}};
int height = matrix.length;
int width = matrix[0].length;
for (int d = height * 2 - 1; d > 0; d--) {
int column = width - 1;
if (d < width) {
column = d - 1;
}
for (int i = d - column - 1; i < height && column >= 0; i++) {
System.out.print(matrix[i][column] + " ");
column--;
}
System.out.println();
}
}
}
你能详细说明输入和输出的要求吗?你总是得到一个nxn的平方矩阵,输出要求是什么?
class Diagonal {
public static void main (String[] args) throws java.lang.Exception {
int[][] matrix = {{20, 19, 18, 17, 16},
{30, 31, 32, 33, 34},
{44, 45, 46, 47, 48},
{74, 55, 76, 77, 78},
{61, 62, 63, 64, 65}};
int height = matrix.length;
int width = matrix[0].length;
for (int d = height * 2 - 1; d > 0; d--) {
int column = width - 1;
if (d < width) {
column = d - 1;
}
for (int i = d - column - 1; i < height && column >= 0; i++) {
System.out.print(matrix[i][column] + " ");
column--;
}
System.out.println();
}
}
}
65
78 64
48 77 63
34 47 76 62
16 33 46 55 61
17 32 45 74
18 31 44
19 30
20