- 詰めswift [無断転載禁止]©2ch.net
31 :デフォルトの名無しさん[]:2016/10/28(金) 07:07:02.16 ID:MsjgJgue - >>30
enum pole:String{ case a,b,c static func restOf(_ a1:pole, _ a2:pole)->pole { var allValue:Set<pole> = [.a,.b,.c] allValue.remove(a1) allValue.remove(a2) return allValue.first! } } func hanoi(num:Int, from:pole, to:pole)->[(pole,pole)]{ var operation:[(pole,pole)]=[] if num == 1 { operation+=[(from,to)] } else { operation += hanoi(num:num-1, from:from, to:.restOf(from,to)) operation += hanoi(num:1, from:from, to:to) operation += hanoi(num:num-1, from:.restOf(from,to), to:to) } return operation } let moveList = hanoi(num:3, from:.a, to:.b) print(moveList.count) print(moveList) String型enumは値を指定しないとenum名が値になるので省略化 workWithはenum自身に第3のポールを判断させればいいので と思ったが帰って長くなってしまった
|