在二维数组Java中绘制数字菱形

我一直在解决一些编码问题以使自己为编码面试做好准备,并发现了一个看起来有点令人费解的问题。我花了一些时间解决了这个问题;然而,代码看起来是硬编码的,没有风格。所以,我想知道我是否可以得到一些关于代码样式的反馈,或者是否可以更好地解决问题。


该问题基本上要求您在二维数组中绘制一个带有图案的数字菱形。它给出了 'x' 的坐标和 x 的范围。从 x 开始,数字一一展开直到范围。因此,有 4 个不同的输入,N(数组的大小)、X、Y('x' 的坐标为(行,列))和 R(范围)。


如果它们的大小为 8,坐标为 (4,5),范围为 3,结果将是,


0 0 0 0 3 0 0 0

0 0 0 3 2 3 0 0

0 0 3 2 1 2 3 0

0 3 2 1 x 1 2 3

0 0 3 2 1 2 3 0

0 0 0 3 2 3 0 0

0 0 0 0 3 0 0 0

0 0 0 0 0 0 0 0

下面是我所拥有的,


    int n = sc.nextInt();

    char[][] arr = new char[n][n];

    int r = sc.nextInt() - 1;

    int c = sc.nextInt() - 1;

    int range = sc.nextInt();


    for (int i = 0; i < n; i++) {

        for (int j = 0; j < n; j++) {

            arr[i][j] = '0';

        }

    }


    arr[r][c] = 'x';



    int num = 1;

    for (int i = 0; i < range; i++) {

        //Cross

        if (c-num > -1) {

            arr[r][c - num] = (char) (num + '0');

        }

        if (c+num < n) {

            arr[r][c + num] = (char) (num + '0');

        }

        if (r-num > -1) {

            arr[r - num][c] = (char) (num + '0');

        }

        if (r+num < n) {

            arr[r + num][c] = (char) (num + '0');

        }

        //Diagonal

        if (i > 0) {

            int sum = num - 1, delta = 1;


            while (sum != 0) {

                if (r-sum > -1 && c+delta < n) {

                    arr[r - sum][c + delta] = (char) (num + '0');

                }

                sum--;

                delta++;

            }

            sum = num - 1; delta = 1;

            while (sum != 0) {

                if (r+sum < n && c-delta > -1) {

                    arr[r + sum][c - delta] = (char) (num + '0');

                }

                sum--;

                delta++;

            }

            sum = num - 1; delta = 1;

            while (sum != 0) {

                if (r-sum > -1 && c-delta > -1) {

                    arr[r - sum][c - delta] = (char) (num + '0');

                }

                sum--;

                delta++;

            }

除了使用四个不同的while循环之外,我想不出任何其他方法来处理对角线数。我将不胜感激任何形式的反馈。提前致谢!


长风秋雁
浏览 210回答 3
3回答

芜湖不芜

这是一个相当紧凑的方法。在每次迭代中,i我们i+1用i+1菱形环填充单个字符宽,以 为中心(row, col),值为i。为了避免填充钻石的内部,我们检查曼哈顿距离 to(row, col)是否等于i- 这仅适用于钻石边界上的单元格。static char[][] buildDiamond(int n, int row, int col, int range){&nbsp; char[][] arr = new char[n][n];&nbsp; for(char[] a : arr) Arrays.fill(a, '0');&nbsp; arr[row][col] = 'x';&nbsp; for(int i=1; i<=range; i++)&nbsp; &nbsp; for(int j=0; j<=i; j++)&nbsp; &nbsp; &nbsp; for(int k=0; k<=i; k++)&nbsp; &nbsp; &nbsp; &nbsp; if(Math.abs(k-j) + Math.abs(k+j-i) == i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[row+k-j][col+k+j-i] += i;&nbsp; return arr;}测试:public static void main(String[] args){&nbsp; for(char[] a : buildDiamond(7, 3, 3, 3))&nbsp;&nbsp; &nbsp; System.out.println(new String(a).replaceAll(".", "$0 "));}输出:0 0 0 3 0 0 0&nbsp;0 0 3 2 3 0 0&nbsp;0 3 2 1 2 3 0&nbsp;3 2 1 x 1 2 3&nbsp;0 3 2 1 2 3 0&nbsp;0 0 3 2 3 0 0&nbsp;0 0 0 3 0 0 0&nbsp;

HUX布斯

您可以只循环一次数组,并根据当前位置(i, j)到固定坐标的相对距离设置值(x, j)。您的代码可能如下所示:import java.util.Arrays;public class Test {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; // variables&nbsp; &nbsp; &nbsp; &nbsp; int n = 8;&nbsp; &nbsp; &nbsp; &nbsp; int x = 4 - 1; // coordinates are one-based&nbsp; &nbsp; &nbsp; &nbsp; int y = 5 - 1; // coordinates are one-based&nbsp; &nbsp; &nbsp; &nbsp; int r = 3;&nbsp; &nbsp; &nbsp; &nbsp; char[][] array = new char[n][n];&nbsp; &nbsp; &nbsp; &nbsp; // logic&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < n; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int dist = Math.abs(x - i) + Math.abs(y - j); // calculate distance&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(dist == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// at x,y&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[i][j] = 'x';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (dist <= r) { // distance to x,y is within range&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[i][j] = (char) (dist + '0');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // distance to x,y is outside of range&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[i][j] = '0';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // dump output&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.deepToString(array)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.replace("], ", "]\n")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.replace("[", "")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.replace("]", "")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.replace(", ", " "));&nbsp; &nbsp; }}这产生以下输出:0 0 0 0 3 0 0 00 0 0 3 2 3 0 00 0 3 2 1 2 3 00 3 2 1 x 1 2 30 0 3 2 1 2 3 00 0 0 3 2 3 0 00 0 0 0 3 0 0 00 0 0 0 0 0 0 0如果你想更简洁,你可以if… else if… else用三元运算符替换分支语句:array[i][j] = dist == 0 ? 'x' : dist <= r ? (char) (dist + '0') : '0';
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java