螺旋形数组

  熬了一下午,今天天气算是入冬以来最好的一天了!呼~,但是,但还是先来看看螺旋形数组吧。

QUESTION DESCRIBE

  从START位置开始,数组中的数值随着紫色的线条的移动方向每经过一格就自增1(只要移动方向上的数值后一个比前一个大就行,不唯一),一直到五角星的位置结束,数据存储在二维数组当中,这就构成了像螺旋一样的数组。

螺旋数组



One of Solutions:

(1) SpiralArray类

public class SpiralArray {

    /**
     * 像图片上显示的那样,可以自定义二维数组的长度,
     * 最终输出一个指定长度的螺旋状数组
     */

    private static Scanner scan = new Scanner(System.in);
    private static int row;    //记录输入的行值
    private static int column;  //记录输入的列值

    public static void main(String[] args) {

        PrintSpiralArray psa = new PrintSpiralArray();
        System.out.print("请输入二维数组的长度(行值 列值):");
        row = scan.nextInt();
        column = scan.nextInt();
        int[][] spiralArray = psa.CreateSpiralArray(row, column);

        for (int i = 0; i < spiralArray.length; i++) {
            for (int j = 0; j < spiralArray[i].length; j++) {
                System.out.print(spiralArray[i][j] + " ");
            }
            System.out.println();
        }
    }
}



(2) PrintSpiralArray类

public class PrintSpiralArray {

    //price用于初始化row和column确定的存储空间的数值
    private int price = 1314 ;   
    private enum Direction{TOP,RIGHT,BOTTOM,LEFT};
    private Direction direct = Direction.RIGHT;
    private int ROW = 0;
    private int COLUMN = 0;

    public int[][] CreateSpiralArray(int rows, int columns) {

        int[][] spiArray = new int[rows][columns];
        int totalPrice = rows*columns + price - 1;

        for (;price <= totalPrice;++price ) {
            spiArray[ROW][COLUMN] = price;
            if (price == totalPrice)   //已经是最后一个值时结束循环 
                break;
            Repositioning(spiArray,ROW,COLUMN,direct);
        }
        return spiArray;
    }

    private void Repositioning
    (int[][] SpiArray, int Row, int Column, Direction dir) 
    {
        switch(dir) {

        case RIGHT:
            if (Column + 1 < SpiArray[Row].length 
            && SpiArray[Row][Column + 1] == 0) {
                this.COLUMN++;
                this.direct = dir;
                return;
            }
            else Repositioning(SpiArray, Row, Column, Direction.BOTTOM);
            break;

        case BOTTOM:
            if (Row + 1 < SpiArray.length 
            && SpiArray[Row + 1][Column] == 0) {
                this.ROW++;
                this.direct = dir;
                return;
            }
            else Repositioning(SpiArray, Row, Column, Direction.LEFT);
            break;

        case LEFT:
            if (Column - 1 >= 0 && 
            SpiArray[Row][Column - 1] == 0) {
                this.COLUMN--;
                this.direct = dir;
                return;
            }
            else Repositioning(SpiArray, Row, Column, Direction.TOP);
            break;

        case TOP:
            if (Row - 1 >= 0 
            && SpiArray[Row - 1][Column] == 0) {
                this.ROW--;
                this.direct = dir;
                return;
            }
            else Repositioning(SpiArray, Row, Column, Direction.RIGHT);
            break;
        }
    }
}





“Nothing is impossible,the word itself says ’ I’m possible ! ‘”

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值