对角线与矩阵

huangapple go评论88阅读模式
英文:

Diagonals with Matrices

问题

目前正在尝试打印矩阵,输出结果为:

0 0 0 0 4
0 0 0 3 0
0 0 2 0 0
0 1 0 0 0
0 0 0 0 0

这是我目前的代码:

for(int row=0; row < matrix.length; row++)
    for(int col = 0; col < matrix[row].length; col++)
        if(row+col == matrix[row].length)

显然还不完整。我甚至不确定代码背后的逻辑是否正确。

英文:

Currently trying to print a matrix with an output of:

0 0 0 0 4
0 0 0 3 0
0 0 2 0 0
0 1 0 0 0
0 0 0 0 0

This is my code currently:

for(int row=0; row &lt; matrix.length; row++)
        for(int col = 0; col &lt; matrix[row].length; col++)
            if(row+col == matrix[row].length)

Obviously not complete. I'm not even sure my logic behind the code is correct.

答案1

得分: 0

假设始终是方阵。这将构建您的矩阵。

for (int row = 0; row < matrix.length; row++) {
    for (int col = 0; col < matrix.length; col++) {
        if (matrix.length - row - 1 == col) {
            matrix[row][col] = col;
        }
    }
}

要在同一步骤中打印,只需删除打印注释。

英文:

Assume always square matrix.This should construct your matrix.
<br>

for(int row=0; row &lt; matrix.length; row++)
{
	//System.out.println(&quot;&quot;);
	for(int col = 0; col &lt; matrix.length; col++)
	{
	    //eg [0][4] = 4 on valid condition [5-0-1] == 4 
	    if(matrix.length-row-1 == col)
	    {
	         matrix[row][col] = col;
	    }
	    //System.out.print(matrix[row][col]);
	 }
}

<br> For printing in same step just remove print-comments

答案2

得分: 0

这是应该能够运行的代码(假设矩阵始终为方阵):

for(int row=0; row < matrix.length; row++){
    for(int col = 0; col < matrix[row].length; col++){
        if(row + col == matrix.length - 1){
            matrix[row][col] = col;
            System.out.print(col + " "); // 打印数值
        } else {
            matrix[row][col] = 0; // 设置数值
            System.out.print("0 ");
        }
    }
    System.out.println("");
}

在条件中我们减去1,因为length-1给出了最后一个索引。(请注意索引从0开始,到length-1为止)

英文:

This is the code that should work (assuming the matrix is always square):

for(int row=0; row &lt; matrix.length; row++){
        for(int col = 0; col &lt; matrix[row].length; col++){
            if(row + col == matrix.length - 1){
                matrix[row][col] = col;
                System.out.print(col+ &quot; &quot;); // Print value
            } else {
                matrix[row][col] = 0; // Set value
                System.out.print(&quot;0 &quot;);
            }
        }
        System.out.println(&quot;&quot;);
    }

We subtracted by 1 in the condition since the length-1 gives us the last index. (reminder that indexes start from 0 and to go length -1)

答案3

得分: 0

请注意,int 数组中的默认值为 0,因为 int 变量的默认值是 0。因此,您只需更改所需对角线上的值,将其余值保持不变。

按以下方式操作:

public class Main {
    public static void main(String[] args) {
        final int SIZE = 5;
        int[][] matrix = new int[SIZE][SIZE];

        // 初始化矩阵
        for (int row = 0; row < matrix.length; row++) {
            for (int col = 0; col < matrix[row].length; col++) {
                if (row + col == matrix.length - 1) {
                    matrix[row][col] = col;
                }
            }
        }

        // 打印矩阵
        for (int row[] : matrix) {
            for (int col : row) {
                System.out.print(col + " ");
            }
            System.out.println();
        }
    }
}

输出:

0 0 0 0 4 
0 0 0 3 0 
0 0 2 0 0 
0 1 0 0 0 
0 0 0 0 0 

我使用了增强型 for 循环来打印矩阵。如果您愿意,您也可以按照以下方式编写:

// 打印矩阵
for (int row = 0; row < matrix.length; row++) {
    for (int col = 0; col < matrix[row].length; col++) {
        System.out.print(matrix[row][col] + " ");
    }
    System.out.println();
}
英文:

Note that the default values in an int arrays are 0 because the default value of an int variable is 0. Therefore, you just need to change the values in the required diagonal and leave the rest of the values as they are.

Do it as follows:

public class Main {
	public static void main(String[] args) {
		final int SIZE = 5;
		int[][] matrix = new int[SIZE][SIZE];

		// Initialise the matrix
		for (int row = 0; row &lt; matrix.length; row++) {
			for (int col = 0; col &lt; matrix[row].length; col++) {
				if (row + col == matrix.length - 1) {
					matrix[row][col] = col;
				}
			}
		}

		// Print the matrix
		for (int row[] : matrix) {
			for (int col : row) {
				System.out.print(col + &quot; &quot;);
			}
			System.out.println();
		}
	}
}

Output:

0 0 0 0 4 
0 0 0 3 0 
0 0 2 0 0 
0 1 0 0 0 
0 0 0 0 0 

I have used the enhanced for loop to print the matrix. You can write it as follows if you wish:

// Print the matrix
for (int row = 0; row &lt; matrix.length; row++) {
	for (int col = 0; col &lt; matrix[row].length; col++) {
		System.out.print(matrix[row][col] + &quot; &quot;);
	}
	System.out.println();
}

huangapple
  • 本文由 发表于 2020年4月10日 03:32:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/61128850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定