総当たり戦の日程作成
Posted feedbacks - Scala
とりあえず。
1 2 3 4 5 6 7 8 9 10 11 | def cycleS[T](a:Seq[T],i:int,j:int) = Stream.const(a).flatMap(v=>v).slice(i,j)
def sportsScheduling(n:int) = {
List((1 to n)).map{ lst =>
val x = n/2-1;val root = lst(0)
(0 to n-2).map{i =>
println(i)
val c = cycleS(cycleS(lst, 1, n), i+x+1, i+x+n)
(root, c(x))::((1 to x).map{j => (c(x+j), c(x-j))}.toList)
}.toList
}.toList
}
|
すみません、いらないprintlnが入ってました。
1 2 3 4 5 6 7 8 9 10 | def cycleS[T](a:Seq[T],i:int,j:int) = Stream.const(a).flatMap(v=>v).slice(i,j)
def sportsScheduling(n:int) = {
List((1 to n)).map{ lst =>
val x = n/2-1;val root = lst(0)
(0 to n-2).map{i =>
val c = cycleS(cycleS(lst, 1, n), i+x+1, i+x+n)
(root, c(x))::((1 to x).map{j => (c(x+j), c(x-j))}.toList)
}.toList
}.toList
}
|
Listを引数にとるバージョン 全組み合わせ出力への1歩 (・・・しかし、型がList[List[(T,T)]]とかなると、やっぱりちょっと見難いなぁ)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | object sche {
def match_sche[T](t:List[T]) = match_sche_loop(List(), t)
def match_sche_loop[T](acc:List[List[(T,T)]], l:List[T]):List[List[(T,T)]] = {
if (acc.length == (l.length-1)) acc
else {
val sp = l.splitAt(l.length/2)
val newacc = sp._1.zip(sp._2.reverse) :: acc
val newl = l.head :: l.tail.tail ::: List(l.tail.head)
match_sche_loop(newacc, newl)
}
}
def main(args: Array[String]) {
val n = args(0).toInt
match_sche((1 to n).toList).foreach {println(_)}
}
}
|





ryugate
#5661()
Rating2/2=1.00
see: カークマンの組分け
[ reply ]