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

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

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

    隨筆 - 30, 文章 - 0, 評論 - 27, 引用 - 0
    數(shù)據(jù)加載中……

    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 石業(yè)海 閱讀(783) 評論(0)  編輯  收藏 所屬分類: 收集類


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 两性色午夜免费视频| 怡红院亚洲红怡院在线观看| 91av免费在线视频| 亚洲人成电影网站国产精品 | 免费A级毛片无码A∨免费| 亚洲国产成人片在线观看无码| 免费人妻精品一区二区三区| 免费精品国产自产拍观看| 亚洲av乱码一区二区三区按摩| 好吊妞788免费视频播放| 日韩亚洲产在线观看| 国产成人免费片在线视频观看| 精品视频免费在线| 国产亚洲美女精品久久久| 国产成人免费AV在线播放| 久久久久久亚洲Av无码精品专口| 久久久久久精品免费看SSS| 亚洲乱码卡一卡二卡三| 国产精品免费播放| 人成电影网在线观看免费| 亚洲色WWW成人永久网址| 四虎国产成人永久精品免费| 亚洲sss综合天堂久久久| 韩国欧洲一级毛片免费| 日韩一级片免费观看| 亚洲av无码国产精品夜色午夜| 国产精品久久免费| 亚洲欧美在线x视频| 亚洲欧洲日产国码无码久久99| 无码国产精品一区二区免费16| 亚洲人成小说网站色| 亚洲А∨精品天堂在线| 无码AV片在线观看免费| 成人亚洲国产va天堂| 国产a v无码专区亚洲av| 免费在线看污视频| 亚洲AV无码片一区二区三区| 国产亚洲一区区二区在线| 丁香花免费完整高清观看| 九九综合VA免费看| 亚洲成在人线电影天堂色|