码迷,mamicode.com
首页 > 编程语言 > 详细

算法(螺旋数字)

时间:2019-01-20 00:05:36      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:移动   circle   system   static   --   i++   class   string   lse   

技术分享图片

 

public class Test {
    public static void main(String[] args) {
        printjz(8);
        System.out.println("======================================");
        printjz(9);
    }


    public static void printjz(int n) {
        int max = n * n;
        int[][] arr = new int[n][n];

        if (n % 2 == 0) {
            int s = n;
            for (int i = n - 1; i >= n / 2; i--) {

                arr[i][n - i - 1] = s * s;
                s -= 2;
            }
            for (int i = 0; i < n / 2; i++) {
                //
                for (int j = i + 1; j < n - i; j++) {
                    int row = n - i - 1;
                    arr[row][j] = arr[row][j - 1] - 1;
                }

                //
                for (int j = n - i - 2; j >= i; j--) {
                    int col = n - i - 1;
                    arr[j][col] = arr[j + 1][col] - 1;
                }

                //
                for (int j = n - i - 2; j >= i; j--) {
                    int row = i;
                    arr[row][j] = arr[row][j + 1] - 1;
                }

                //
                for (int j = i + 1; j < n - 1 - i; j++) {
                    int col = i;
                    arr[j][col] = arr[j - 1][col] - 1;
                }
            }

        } else {
            int s = n;
            for (int i = 0; i <= n / 2; i++) {
                arr[i][n - i - 1] = s * s;
                s -= 2;
            }
            for (int i = 0; i < (n - 1) / 2; i++) {
                //
                for (int j = n - i - 2; j >= i; j--) {
                    int row = i;
                    arr[row][j] = arr[row][j + 1] - 1;
                }

                //
                for (int j = i + 1; j < n - i; j++) {
                    int col = i;
                    arr[j][col] = arr[j - 1][col] - 1;
                }

                //
                for (int j = i + 1; j < n - i; j++) {
                    int row = n - i - 1;
                    arr[row][j] = arr[row][j - 1] - 1;
                }

                //
                for (int j = n - i - 2; j >= i + 1; j--) {
                    int col = n - i - 1;
                    arr[j][col] = arr[j + 1][col] - 1;
                }
            }


        }

        for (int k = 0; k < arr.length; k++) {
            for (int j = 0; j < arr[k].length; j++) {
                System.out.print(arr[k][j] + "\t");
                if (j == arr[k].length - 1) {
                    System.out.println("\n");
                }
            }
        }
    }
    public static void printNum(int n) {
        int max = n * n;
        int arr[][] = new int[n][n];
        int circle = 2, row = n / 2, col = n / 2;
        if (n % 2 == 0) {

        } else {

        }

        arr[row][col] = 1;
        int num = 1;
        for (int i = n / 2; i > 0; i--) {
            //向右移动一个
            num++;
            col++;
            arr[row][col] = num;
            //
            for (int j = 0; j < circle - 1; j++) {
                num++;
                //向下移动一个
                row++;
                arr[row][col] = num;
            }
            for (int j = 0; j < circle; j++) {
                num++;
                //向左移动
                col--;
                arr[row][col] = num;
            }
            for (int j = 0; j < circle; j++) {
                num++;
                //向上移动
                row--;
                arr[row][col] = num;
            }


            for (int j = 0; j < circle; j++) {
                num++;
                //向右移动
                col++;
                arr[row][col] = num;
            }
            circle += 2;
        }

        for (int k = 0; k < arr.length; k++) {
            for (int j = 0; j < arr[k].length; j++) {
                System.out.print(arr[k][j] + "\t");
                if (j == arr[k].length - 1) {
                    System.out.println("\n");
                }
            }
        }
    }
}

 

算法(螺旋数字)

标签:移动   circle   system   static   --   i++   class   string   lse   

原文地址:https://www.cnblogs.com/code111/p/10293676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!