//: Playground - noun: a place where people can play
import UIKit
var n = 30;//求Kolakoski数列第n项的值
var set1 = [2,1,3,1];
var arr = Array<Int>();
func add1(element:Int,times:Int){
for _ in 0..<times{
arr.append(element)
}
}
add1(element: set1[0], times: set1[0])
var i = 1//第一次插入
//i是当前插入动作的次数,i有两个映射关系,i首先映射要插入arr的数的值,第二个映射i决定arr[i],arr[i]决定插入的次数
while arr.count < n {
add1(element: set1[i%(set1.count)], times: arr[i])
i=i+1//插入次数+1
}
print(arr)
//2, 2, 1, 1, 3, 1, 2, 2, 2, 1, 3, 3, 1, 1, 2, 2, 1, 3, 3, 3, 1, 1, 1, 2, 1, 3, 3, 1, 1, 2, 1, 1, 1, 3, 3, 3, 1, 1, 1, 2, 1, 3, 1, 1, 2, 1, 1, 1, 3, 3, 3, 1, 2, 1, 1, 3, 1, 2, 1, 1, 1, 3, 3, 3, 1, 1, 1, 2, 1, 3, 1, 1, 2, 1, 1, 1, 3, 1, 2, 2, 1, 3, 1, 2, 2, 2, 1, 1, 1, 3, 3, 3, 1, 2, 2, 1, 3, 1, 1, 1
网友评论