<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 30, 文章 - 0, 評論 - 27, 引用 - 0
    數據加載中……

    java打印螺旋方陣

    第一種辦法
     1import java.text.DecimalFormat;
     2
     3public class HelixMatrix {
     4    private int[][] array;
     5    private int m,n;
     6    public HelixMatrix(int m, int n) {
     7        this.m = m;
     8        this.n = n;
     9        array = new int[m][];
    10        for(int i = 0; i < m; i++)
    11            array[i] = new int[n];
    12    }

    13
    14    private void fill() {
    15        int count = m * n;
    16        int direct = 0;
    17        int round = 1;
    18        for(int index = 1, x = 0, y = 0; index <= count; index++{
    19            array[x][y] = index;
    20            switch(direct) {
    21                case 0//向右
    22                    if(y < n - round)
    23                        y++;
    24                    else {
    25                        direct = 1;
    26                        x++;
    27                    }

    28                    break;
    29                case 1:
    30                    if(x < m - round)
    31                        x++;
    32                    else {
    33                        direct = 2;
    34                        y--;
    35                    }

    36                    break;
    37                case 2:
    38                    if(y >= round)
    39                        y--;
    40                    else {
    41                        direct = 3;
    42                        x--;
    43                    }

    44                    break;
    45                case 3:
    46                    if(x > round)
    47                        x--;
    48                    else {
    49                        direct = 0;
    50                        round++;
    51                        y++;
    52                    }

    53                    break;                        
    54            }

    55        }

    56    }

    57
    58    private void printMatrix() {
    59        DecimalFormat df = new DecimalFormat(" 000");
    60        for(int i = 0; i < m; i++{
    61            for(int j = 0; j < n; j++)
    62                System.out.print(df.format(array[i][j]));
    63            System.out.println();
    64        }

    65    }

    66    
    67    public static void main(String[] args) {
    68        HelixMatrix matrix = new HelixMatrix(8,6);
    69        matrix.fill();
    70        matrix.printMatrix();
    71    }

    72}

    第二種辦法
     1public class ScrewSquare {
     2
     3    public static void main(String[] args) {
     4        int w = 5;
     5        int h = 5;
     6        int[][] array = new int[w][h];
     7        fill(array, 00, w-1, h-11);
     8        for (int y=0; y<h; y++{
     9            for (int x=0; x<w; x++{
    10                System.out.printf("%2d ", array[x][y]);
    11            }

    12            System.out.println();
    13        }

    14    }

    15
    16    public static void fill(int[][] array, int left, int top, int right, int bottom, int start) {
    17        if (left > right || top > bottom) return;
    18
    19        for (int x=left; x<=right; x++)
    20            array[x][top] = start++;
    21
    22        for (int y=top+1; y<=bottom; y++)
    23            array[right][y] = start++;
    24
    25        for (int x=right-1; x>=left; x--)
    26            array[x][bottom] = start++;
    27
    28        for (int y=bottom-1; y>=top+1; y--)
    29            array[left][y] = start++;
    30
    31        fill(array, left+1, top+1, right-1, bottom-1, start);
    32    }

    33}

    posted on 2008-01-17 21:38 石業海 閱讀(783) 評論(0)  編輯  收藏 所屬分類: 收集類

    主站蜘蛛池模板: 无码中文在线二区免费| 免费A级毛片无码A∨免费| 日本无卡码免费一区二区三区| 亚洲人成免费电影| 91禁漫免费进入| 亚洲精品中文字幕麻豆| 亚洲大片免费观看| 亚洲三级视频在线| 夭天干天天做天天免费看| 精品久久久久久亚洲综合网| 四虎永久成人免费影院域名| 日韩在线观看视频免费| 成人午夜亚洲精品无码网站| 野花香高清在线观看视频播放免费| 亚洲国产精品一区二区久久hs| 亚洲欧洲免费视频| 亚洲伊人色一综合网| 精品久久免费视频| caoporn成人免费公开| 亚洲av午夜福利精品一区人妖| 18禁止看的免费污网站| 亚洲熟妇AV一区二区三区浪潮| 国产jizzjizz免费视频| 中文无码日韩欧免费视频| 91精品国产亚洲爽啪在线观看| 搡女人免费视频大全| 一个人晚上在线观看的免费视频| 人人狠狠综合久久亚洲88| 亚洲免费观看网站| 老妇激情毛片免费| 亚洲av福利无码无一区二区 | 亚洲AV无码成人精品区狼人影院 | 亚洲精品高清在线| 一级毛片免费观看| 亚洲国产成人无码AV在线影院| 久久久久亚洲AV成人网人人网站| 久久精品人成免费| 国产亚洲福利精品一区二区 | 亚洲午夜电影在线观看高清| 国产精品美女自在线观看免费| 西西人体免费视频|