用java打印螺旋矩阵

发布于 2025-01-11 18:42:06 字数 1325 浏览 0 评论 0原文

我正在尝试使用接受整数的方法来打印螺旋矩阵。然而,我在纸上的代码工作正常,但是当我运行时,我得到不同的数字来代替我想要的数字。

public class ConstructSpiral{
    public static void main(String[] args) {

        constructSpiral(3);
    }

    public static int[][] constructSpiral(int n) {
        
        int[][] matrix = new  int[n][n];
        int sum = 0;
        int i = matrix.length - 1;
        int j = matrix[i].length - 1;
        
        for(j = matrix[i].length - 1; j >= 0; j--)
        {
            sum += sum + 1;
            matrix[i][j] = sum;
        }
        
        j = 0;
        for(i = (matrix.length - 1) - 1; i >= 0; i--) {
            sum += sum + 1;
            matrix[i][j] = sum;
        }

        i = 0;
        for(j = 1;  j < matrix.length; j++)
        {
            sum += sum + 1;
            matrix[i][j] = sum;
        }

        i = 1;
        for(j = 2; j <= i; j--){
            sum += sum + 1;
            matrix[i][j] = sum;
        }

        for(i = matrix.length - 1; i >= 0; i--) {
            for(j = matrix[i].length - 1; j >= 0; j--) 
            {
                System.out.print(matrix[i][j] + ", ");
            }
            System.out.println();
        }

        return matrix;

    }
}

实际上,它应该打印类似

5 6 7
4 9 8
3 2 1

In help 的内容,我们将不胜感激。

I'm trying to print a spiral matrix using a method that takes in an integer. However, my code on paper works fine, but when I run I get different numbers in place of the number I want.

public class ConstructSpiral{
    public static void main(String[] args) {

        constructSpiral(3);
    }

    public static int[][] constructSpiral(int n) {
        
        int[][] matrix = new  int[n][n];
        int sum = 0;
        int i = matrix.length - 1;
        int j = matrix[i].length - 1;
        
        for(j = matrix[i].length - 1; j >= 0; j--)
        {
            sum += sum + 1;
            matrix[i][j] = sum;
        }
        
        j = 0;
        for(i = (matrix.length - 1) - 1; i >= 0; i--) {
            sum += sum + 1;
            matrix[i][j] = sum;
        }

        i = 0;
        for(j = 1;  j < matrix.length; j++)
        {
            sum += sum + 1;
            matrix[i][j] = sum;
        }

        i = 1;
        for(j = 2; j <= i; j--){
            sum += sum + 1;
            matrix[i][j] = sum;
        }

        for(i = matrix.length - 1; i >= 0; i--) {
            for(j = matrix[i].length - 1; j >= 0; j--) 
            {
                System.out.print(matrix[i][j] + ", ");
            }
            System.out.println();
        }

        return matrix;

    }
}

In reality, it should print something like

5 6 7
4 9 8
3 2 1

In help would greatly be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2025-01-18 18:42:06

我认为您走在正确的轨道上,您可能只想多利用 constructSpiral 方法的 n 参数,并将打印逻辑移至不同的方法中:

import java.util.Scanner;

public class ConstructSpiral {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter value of n: ");
        int n = scanner.nextInt();
        int[][] spiral = constructSpiral(n);
        printSquareMatrix(spiral, n);
    }

    public static int[][] constructSpiral(int n) {
        int[][] matrix = new int[n][n];
        int sum = 1;
        int r1 = 0, c1 = 0, r2 = n - 1, c2 = n - 1;
        int i = 0, j = 0;
        while (sum <= n * n) {
            for (j = c2; j >= c1; j--)
                matrix[r2][j] = sum++;
            r2--;
            for (i = r2; i >= r1; i--)
                matrix[i][c1] = sum++;
            c1++;
            for (j = c1; j <= c2; j++)
                matrix[r1][j] = sum++;
            r1++;
            for (i = r1; i <= r2; i++)
                matrix[i][c2] = sum++;
            c2--;
        }
        return matrix;
    }

    public static void printSquareMatrix(int[][] matrix, int n) {
        int padLength = String.valueOf(n * n).length() + 1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.printf("%1$-" + padLength + "s", matrix[i][j]);
            }
            System.out.println();
        }
    }

}

用法示例 1:

Enter value of n: 3
5 6 7 
4 9 8 
3 2 1 

用法示例 2:

Enter value of n: 5
9  10 11 12 13 
8  21 22 23 14 
7  20 25 24 15 
6  19 18 17 16 
5  4  3  2  1 

用法示例 3:

Enter value of n: 10
19  20  21  22  23  24  25  26  27  28  
18  51  52  53  54  55  56  57  58  29  
17  50  75  76  77  78  79  80  59  30  
16  49  74  91  92  93  94  81  60  31  
15  48  73  90  99  100 95  82  61  32  
14  47  72  89  98  97  96  83  62  33  
13  46  71  88  87  86  85  84  63  34  
12  45  70  69  68  67  66  65  64  35  
11  44  43  42  41  40  39  38  37  36  
10  9   8   7   6   5   4   3   2   1 

I think you are on the right track you might just want to utilize the n parameter of your constructSpiral method a bit more, as well as move the printing logic into a different method:

import java.util.Scanner;

public class ConstructSpiral {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter value of n: ");
        int n = scanner.nextInt();
        int[][] spiral = constructSpiral(n);
        printSquareMatrix(spiral, n);
    }

    public static int[][] constructSpiral(int n) {
        int[][] matrix = new int[n][n];
        int sum = 1;
        int r1 = 0, c1 = 0, r2 = n - 1, c2 = n - 1;
        int i = 0, j = 0;
        while (sum <= n * n) {
            for (j = c2; j >= c1; j--)
                matrix[r2][j] = sum++;
            r2--;
            for (i = r2; i >= r1; i--)
                matrix[i][c1] = sum++;
            c1++;
            for (j = c1; j <= c2; j++)
                matrix[r1][j] = sum++;
            r1++;
            for (i = r1; i <= r2; i++)
                matrix[i][c2] = sum++;
            c2--;
        }
        return matrix;
    }

    public static void printSquareMatrix(int[][] matrix, int n) {
        int padLength = String.valueOf(n * n).length() + 1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.printf("%1$-" + padLength + "s", matrix[i][j]);
            }
            System.out.println();
        }
    }

}

Example Usage 1:

Enter value of n: 3
5 6 7 
4 9 8 
3 2 1 

Example Usage 2:

Enter value of n: 5
9  10 11 12 13 
8  21 22 23 14 
7  20 25 24 15 
6  19 18 17 16 
5  4  3  2  1 

Example Usage 3:

Enter value of n: 10
19  20  21  22  23  24  25  26  27  28  
18  51  52  53  54  55  56  57  58  29  
17  50  75  76  77  78  79  80  59  30  
16  49  74  91  92  93  94  81  60  31  
15  48  73  90  99  100 95  82  61  32  
14  47  72  89  98  97  96  83  62  33  
13  46  71  88  87  86  85  84  63  34  
12  45  70  69  68  67  66  65  64  35  
11  44  43  42  41  40  39  38  37  36  
10  9   8   7   6   5   4   3   2   1 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文