JAVA 中 N 维向量的主对角线距离问题

输入 int n 和int width
n维矩阵 中 与主对角线小于width的元素全部由星号(*)代替

img

img


怎么定义这个N维数组和元素到对角线的距离啊

这个是java版的

static int[][] matrix = new int[4][4];
public static void main(String[] args) {
    bandMatrix(4,1);
    showMatrix(matrix);
}
public static void bandMatrix(int n,int width){
    for(int y = 0;y<matrix.length;y++){
        int[] row = matrix[y];
        for(int x = 0;x<row.length;x++){
            if(Math.abs(x - y)<width+1){
                row[x] = 1;
            }
        }
    }
}
public static void showMatrix(int[][] a){
    for (int[] ints : a) {
        System.out.println(Arrays.toString(ints));
    }
}