美文网首页iOS Developer
swift 打印回字形

swift 打印回字形

作者: 34码的小孩子 | 来源:发表于2017-03-22 16:16 被阅读32次

    下图是输入 n = 9时输出的回字形效果图


    效果图

    想法

    • 输出是正方形,N行N列,而且数字是从外圈到里圈递增加1。
    • 以一圈为一个基本单位,写一个方法实现
    • 递归调用上面的方法,直到到达最中间的位置。
    • 用一个二维数组存储数据,当N值固定了之后,数组的长度也就固定了

    实现

    整个过程是在一个struct 里面完成。
    备注:如果遍历数组需要使用下标和数组元素,则用元组形式遍历

    for (index,curItem) in array.enumerated() 
    

    struct的结构如下:

    import Foundation
    struct LoopTest {
    private var column : Int = 0
    private var printArray : [[Int]]
    
    //根据用户输入N值初始化,初始化时必须给所有的属性赋值
    init(arrayColumn: Int) {
        column = arrayColumn
        printArray = [[]]
    }
    
    //初始化二维数组
    private mutating func setPrintArray() {
        //因为在init方法里面用[[]]的二维数组初始化,printArray已经有了第一个
        //元素——空数组,所以这里是赋值,而不是添加。
        let firstArray = Array(repeating: 0, count: column)
        printArray[0] = firstArray
        
        for _ in 1..<column {
            let array = Array(repeating: 0, count: column)
            
            printArray.append(array)
        }
    }
    
    /// 画一圈
    ///
    /// - parameter round: 圈数
    /// - parameter item:  数值
    ///
    /// - returns: 下一圈的第一个数值
    private mutating func setOneArround(round: Int, item: Int) -> Int {
       //其中curItem ==  0判断是否已经赋值
        var currentItem = item
        //上横,取第round圈的最上面一行的数组
        var array = printArray[round - 1]
        
        for (index,curItem) in array.enumerated() {
            if curItem == 0 {
                array[index] = currentItem
                currentItem = currentItem + 1
            }
        }
        
        printArray[round - 1] = array
        
        //右竖
        for index in round..<printArray.count {
            var itemArray = printArray[index]
            //取第round圈最右一列
            if itemArray[column - round] == 0 {
                itemArray[column - round] = currentItem
                currentItem = currentItem + 1
                
                printArray[index] = itemArray
            }
        }
        
        //下横,取第round圈的最下面一行的数组
        array = printArray[column - round]
        
        //反向进行遍历数组
        for (index,curItem) in array.enumerated().reversed() {
            if curItem == 0 {
                array[index] = currentItem
                currentItem = currentItem + 1
            }
        }
        
        printArray[column - round] = array
        
        //左竖
        for index in round..<printArray.count {
            var itemArray = printArray[column - index]
            //取第round圈最左一列
            if itemArray[round - 1] == 0 {
                itemArray[round - 1] = currentItem
                currentItem = currentItem + 1
                
                printArray[column - index] = itemArray
            }
        }
        
        return currentItem
    }
    }
    

    最后为了输出比较规矩,需要对数字进行添加空格占位符,根据当前数值与最大数值的长度差添加空格

    //获取数值的长度
    private func getLength(item: Int) -> Int {
        var index = item
        var length = 0
        
        repeat {
            index = index / 10
            length = length + 1
        }while index >= 1
        
        return length
    }
    
    //构造回字形二维数组
    private mutating func getArroundPrintArray() {
        setPrintArray()
        var index = 1
        
        for i in 1...column {
            index = setOneArround(round: i, item: index)
        }
    }
    
      //唯一的对外方法,打印回字形
       mutating func printArround() {
        getArroundPrintArray()
        let length = getLength(item: column * column)
        
        for i in 1...column {
            let array = printArray[i - 1]
            for item in array {
                let spaceLength = length - getLength(item: item)
                var space = ""
                
                if spaceLength >= 1 {
                    for _ in 1...spaceLength {
                        space += " "
                    }
                }
                
                print ("\(space)\(item)", terminator: " ")
            }
            
            print()
        }
        
        print()
    }
    

    相关文章

      网友评论

        本文标题:swift 打印回字形

        本文链接:https://www.haomeiwen.com/subject/sdsonttx.html