Comment detail

議席数をドント方式で (Nested Flatten)
とりあえず普通に。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import scala.collection.mutable.ListBuffer
import scala.util.Sorting

def dont(total:int, votes:Array[int]):Array[int] = {
  val seats = Array.make(votes.length, 0)
  val sorted = (1 to total).foldLeft(new ListBuffer[(double,int)]) { (r, i) => 
    (0 to votes.size-1).foreach(j => r += (votes(j).toDouble/i, j));r
  }.toArray
  Sorting.stableSort(sorted)
  sorted.reverse.slice(0,100).foreach(x => seats(x._2) = seats(x._2) + 1)
  seats
}

dont(100, Array(123, 4, 56, 78))
すみません、1行ミスってました。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import scala.collection.mutable.ListBuffer
import scala.util.Sorting

def dont(total:int, votes:Array[int]):Array[int] = {
  val seats = Array.make(votes.length, 0)
  val sorted = (1 to total).foldLeft(new ListBuffer[(double,int)]) { (r, i) => 
    (0 to votes.size-1).foreach(j => r += (votes(j).toDouble/i, j));r
  }.toArray
  Sorting.stableSort(sorted)
  sorted.reverse.slice(0,total).foreach(x => seats(x._2) = seats(x._2) + 1)
  seats
}

dont(10, Array(15000, 8000, 6500, 2500, 1000, 500)).foreach(println(_))

Index

Feed

Other

Link

Pathtraq

loading...