This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> class FInt(n: Int){ | |
| val factors: List[Int] = { | |
| def factorsR(n: Int, fs: List[Int]): List[Int] = n match{ | |
| case 1 => fs | |
| case s => { | |
| val divn = ps.find(n%_ == 0).get | |
| factorsR(n/divn, divn +: fs) | |
| } | |
| } | |
| factorsR(n, Nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> lazy val twiceSquareStream = Stream.from(0).map(n => 2 * math.pow(n,2).toInt) | |
twiceSquareStream: scala.collection.immutable.Stream[Int] = <lazy> | |
scala> def isPrimeWithSquare(n: Int) = { | |
| val tss = twiceSquareStream.takeWhile(_ < n) | |
| tss.exists(ts => isPrime(n-ts)) | |
| } | |
isPrimeWithSquare: (n: Int)Boolean | |
scala> lazy val oddStream: Stream[Int] = Stream.from(3).filter(_%2 != 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> lazy val ps: Stream[Int] = 2 #:: Stream.from(3).filter(i => ps.takeWhile(j => j*j <= i).forall(i%_ != 0)) | |
ps: Stream[Int] = <lazy> | |
scala> def isPrime(n: Int) = n match{ | |
| case i if i <= 1 => false | |
| case i => ps.takeWhile(j => j*j <= i).forall(n%_ != 0) | |
| } | |
isPrime: (n: Int)Boolean | |
scala> def isPrimeTakeLeft(n: Int) = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type -->[A, B] = PartialFunction[A, B] | |
// 追記:これPartialFunctionじゃなくて普通にmapで良いやん... | |
lazy val fizzbuzzIndex: Int --> (Int, String) = (x: Int) => (x % 3, x % 5) match { | |
case (0, 0) => (x, "fizzbuzz") | |
case (_, 0) => (x, "buzz") | |
case (0, _) => (x, "fizz") | |
case _ => (x, "") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> type -->[A,B] = PartialFunction[A,B] | |
scala> val fizzbuzz: Int --> String = ((x: Int) => (x%3, x%5) match { | |
| case (0, 0) => "fizzbuzz" | |
| case (_, 0) => "buzz" | |
| case (0, _) => "fizz" | |
| }) | |
fizzbuzz: -->[Int,String] = <function1> | |
scala> (1 to 100).collect(fizzbuzz) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://acm.pku.edu.cn/JudgeOnline/problem?id=2685 | |
package icpc | |
import java.util.Scanner | |
class Numeral(sc: Scanner){ | |
val cnv = Map(1000 -> "m", 100 -> "c", 10 -> "x", 1 -> "i") | |
def encode(n: Int): String = { | |
val mcxi = List((1000,n/1000), (100,n%1000/100), (10,n%1000%100/10), (1,n%1000%100%10)) | |
(mcxi filter {_._2 != 0} map {n => n._2 + cnv.get(n._1).get}).mkString.replace("1","") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://acm.pku.edu.cn/JudgeOnline/problem?id=2029 | |
package icpc | |
import java.util.Scanner | |
class Persimmon(sc: Scanner) { | |
def run(): Unit = { | |
def search(n: (Int, Int), trees: Seq[(Int, Int)], s: Int, t: Int): Int = | |
(for(i <- 0 until s; j <- 0 until t if (trees.contains((n._1+i,n._2+j)))) yield {}).length | |
while(true) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://www.deqnotes.net/acmicpc/p0006/judge | |
package icpc | |
import java.util.Scanner | |
class Purse(sc: Scanner){ | |
def solve(amount: Int, own: Int): (Int, Int, Int, Int) = { | |
val diff = own - amount | |
(diff%500%100%50/10, diff%500%100/50, diff%500/100, diff/500) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://acm.pku.edu.cn/JudgeOnline/problem?id=2028 | |
// 配列のindexなどの使い方がいまいち分からない. | |
package icpc | |
import java.util.Scanner | |
class Meeting(sc: Scanner){ | |
def adjust(n: Int, q: Int): Int = { | |
val dates = Array.tabulate(101)(n => 0) | |
for (i <- 1 to n) { | |
val c = sc.nextInt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://www.deqnotes.net/acmicpc/p0004/judge | |
package icpc | |
import java.util.Scanner | |
import scala.annotation.tailrec | |
class ExploringCaves(sc: Scanner) { | |
def findTreasure(routes: List[(Int, Int)]) = routes.sortBy(n => (n._1*n._1 + n._2*n._2)).last | |
def search(start: (Int, Int)): (Int, Int) = { | |
@tailrec |