lunlumo #7696(2008/09/21 10:58 GMT) [ Scala ] Rating0/0=0.00
#7638と#7671のやり方で。
see: 「レッツ・チャレンジ! パソコン甲子園」第11回の解答例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
import scala.collection.immutable.SortedSet import scala.collection.immutable.TreeSet abstract class CHammingNumbers { def take(n:Int):List[Int] } class CHammingNumbersG extends CHammingNumbers { def next(s:List[Int],h:List[Int]):Tuple2[List[Int],List[Int]] = { val m:List[Int] = s.zip(List(2,3,5)).map { v => h.apply(v._1)*v._2 } val n:Int = m.sort { (a,b) => a < b }.head (s.zip(m).map { v => (v._2 == n) match { case true => v._1 + 1; case _ => v._1 } },h+n) } def take(n:Int,s:List[Int],h:List[Int]):Tuple2[List[Int],List[Int]] = n match { case 0 => (s,h) case _ => next(s,h) match { case v => take(n-1,v._1,v._2) } } def take(n:Int):List[Int] = take(n-1,List(0,0,0),List(1))._2 } class CHammingNumbersS extends CHammingNumbers { def next(c:SortedSet[Int]):Tuple2[Int,SortedSet[Int]] = (c.firstKey,(TreeSet(c.firstKey*2,c.firstKey*3,c.firstKey*5)++(c-c.firstKey))) def take(n:Int,c:SortedSet[Int]):List[Int] = n match { case 0 => List() case _ => next(c) match { case v => v._1::take(n-1,v._2) } } def take(n:Int):List[Int] = take(n,TreeSet(1)) } object HammingNumbers { def main(args:Array[String]):Unit = { try { val n:Int = args.length match { case 1 => args(0).toInt case _ => 100 } List(new CHammingNumbersG,new CHammingNumbersS).foreach { h => println(h.take(n).mkString("\n")) } } catch { case e => e.printStackTrace } } }
Rating0/0=0.00-0+
[ reply ]
lunlumo #7696() [ Scala ] Rating0/0=0.00
#7638と#7671のやり方で。
see: 「レッツ・チャレンジ! パソコン甲子園」第11回の解答例
import scala.collection.immutable.SortedSet import scala.collection.immutable.TreeSet abstract class CHammingNumbers { def take(n:Int):List[Int] } class CHammingNumbersG extends CHammingNumbers { def next(s:List[Int],h:List[Int]):Tuple2[List[Int],List[Int]] = { val m:List[Int] = s.zip(List(2,3,5)).map { v => h.apply(v._1)*v._2 } val n:Int = m.sort { (a,b) => a < b }.head (s.zip(m).map { v => (v._2 == n) match { case true => v._1 + 1; case _ => v._1 } },h+n) } def take(n:Int,s:List[Int],h:List[Int]):Tuple2[List[Int],List[Int]] = n match { case 0 => (s,h) case _ => next(s,h) match { case v => take(n-1,v._1,v._2) } } def take(n:Int):List[Int] = take(n-1,List(0,0,0),List(1))._2 } class CHammingNumbersS extends CHammingNumbers { def next(c:SortedSet[Int]):Tuple2[Int,SortedSet[Int]] = (c.firstKey,(TreeSet(c.firstKey*2,c.firstKey*3,c.firstKey*5)++(c-c.firstKey))) def take(n:Int,c:SortedSet[Int]):List[Int] = n match { case 0 => List() case _ => next(c) match { case v => v._1::take(n-1,v._2) } } def take(n:Int):List[Int] = take(n,TreeSet(1)) } object HammingNumbers { def main(args:Array[String]):Unit = { try { val n:Int = args.length match { case 1 => args(0).toInt case _ => 100 } List(new CHammingNumbersG,new CHammingNumbersS).foreach { h => println(h.take(n).mkString("\n")) } } catch { case e => e.printStackTrace } } }Rating0/0=0.00-0+
[ reply ]