Comment detail

分数を小数に展開 (Nested Flatten)

	
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import scala.collection.mutable._
def f(a:Int, b:Int):String = {
  var result = new ListBuffer[Int]
  val mods   = new LinkedHashSet[Int]
  mods += a%b
  def l(v:Int):Int = {
    result += (v*10)/b
    val mod = v*10%b
    if(mods.contains(mod)) mods.indexOf(mod)
    else if(mod == 0) -1 
    else {mods+=mod;l(mod)}
  }
  val i = l(a)
  var p = result.mkString("")
  if(i != -1) p = p.substring(0,i)+"{"+p.substring(i)+"}"
  "0."+p
}

Index

Feed

Other

Link

Pathtraq

loading...