zoukankan      html  css  js  c++  java
  • Java实现蛇形矩阵

    public class Solution {
        //下x++  左y--  上x--  右y++
        public void prints(int n) {
            int[][] mp = new int[n][n];
            for(int i=0; i<n; i++) {
                for(int j=0; j<n; j++) {
                    mp[i][j] = 0;
                }
            }
            int x , y, tot; 
            tot = mp[x=0][y=n-1] = 1;
            while(tot < n*n) {
                while(x+1<n && mp[x+1][y]==0) mp[++x][y] = ++tot;
                while(y-1>=0 && mp[x][y-1]==0) mp[x][--y] = ++tot;
                while(x-1>=0 && mp[x-1][y]==0) mp[--x][y] = ++tot;
                while(y+1<n && mp[x][y+1]==0) mp[x][++y] = ++tot;
            }
            for(int i=0; i<n; i++) {
                for(int j=0; j<n; j++) {
                    System.out.print(mp[i][j] + " ");
                }
                System.out.println();
            }
        }
        public static void main(String[] args) {
            Solution solution = new Solution();
            solution.prints(8);
        }
    }



    /*

    22 23 24 25 26 27 28 1
    21 44 45 46 47 48 29 2
    20 43 58 59 60 49 30 3
    19 42 57 64 61 50 31 4
    18 41 56 63 62 51 32 5
    17 40 55 54 53 52 33 6
    16 39 38 37 36 35 34 7
    15 14 13 12 11 10 9 8

    */
  • 相关阅读:
    匹配@之前面的部分
    把一个数字的字符串转换为千分符的标识方式?
    下标重置
    linux的time命令

    常用正则
    正则
    PHP 菠菜木马代码
    PHP 木马代码,
    一句话的木马
  • 原文地址:https://www.cnblogs.com/Roni-i/p/10434418.html
Copyright © 2011-2022 走看看