結果

問題 No.211 素数サイコロと合成数サイコロ (1)
ユーザー yoshikyotoyoshikyoto
提出日時 2015-05-22 22:25:45
言語 Scala(Beta)
(2.13.3)
結果
AC  
実行時間 635 ms / 1,000 ms
コード長 3,191 Byte
コンパイル時間 8,299 ms
使用メモリ 58,368 KB
最終ジャッジ日時 2020-09-26 14:37:50
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 635 ms
54,728 KB
testcase_01 AC 603 ms
58,368 KB
testcase_02 AC 635 ms
57,080 KB
testcase_03 AC 603 ms
56,156 KB
testcase_04 AC 618 ms
54,856 KB
testcase_05 AC 615 ms
58,000 KB
testcase_06 AC 626 ms
54,824 KB
testcase_07 AC 608 ms
54,752 KB
testcase_08 AC 604 ms
54,868 KB
testcase_09 AC 606 ms
57,952 KB
testcase_10 AC 611 ms
57,032 KB
testcase_11 AC 600 ms
54,580 KB
testcase_12 AC 612 ms
54,744 KB
testcase_13 AC 608 ms
55,144 KB
testcase_14 AC 615 ms
54,696 KB
testcase_15 AC 602 ms
54,896 KB
testcase_16 AC 620 ms
56,780 KB
testcase_17 AC 619 ms
55,244 KB
testcase_18 AC 609 ms
55,124 KB
testcase_19 AC 607 ms
54,888 KB
testcase_20 AC 606 ms
56,364 KB
testcase_21 AC 611 ms
54,836 KB
testcase_22 AC 610 ms
54,688 KB
testcase_23 AC 626 ms
55,016 KB
testcase_24 AC 610 ms
56,536 KB
testcase_25 AC 626 ms
56,548 KB
testcase_26 AC 602 ms
56,324 KB
testcase_27 AC 601 ms
54,884 KB
testcase_28 AC 615 ms
54,824 KB
testcase_29 AC 617 ms
54,724 KB
testcase_30 AC 609 ms
57,904 KB
testcase_31 AC 601 ms
54,988 KB
testcase_32 AC 611 ms
54,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val k = sc.nextInt();
    val p: List[Int] = List(2, 3, 5, 7, 11, 13)
    val q: List[Int] = List(4, 6, 8, 9, 10, 12)
    
    var cnt = 0;
    for(i <- p; j <- q) yield {
      if(i * j == k) cnt += 1;
    }
    println(cnt.toDouble / (p.length * q.length).toDouble)
  }
  
  
  /**
   * P16
   */
  def drop[T](n: Int, list: List[T]): List[T] = {
    def f(i: Int)(n: Int, list: List[T]): List[T] = (i, n, list) match {
      case (_, _, Nil) => Nil
      case (1, n, car :: cdr) => f(n)(n, cdr)
      case (i, n, car :: cdr) => car :: f(i-1)(n, cdr)
    }
    f(n)(n, list)
  }
  /*
  def d[T](i: Int, n: Int, list: List[T]): List[T] = (i, n, list) match {
    case (_, _, Nil) => Nil
    case (1, n, car :: cdr) => d(n, n, cdr)
    case (i, n, car :: cdr) => car :: d(i-1, n, cdr)
  }
  */
  
  /**
   * P12
   */
  /*
  def decode[T](list: List[List[T]]): List[T] = list match {
    case Nil => Nil
    case List(n, v) :: cdr => 
      for(i <- 1 to n) yield {v} :: cdr
  }
  */
  
  /**
   * P11
   */
  def encode[T](list: List[T]): List[(Int, T)] = list match {
    case Nil => Nil
    case car :: cdr => {
      val (first, second) = list.span(x => x == car)
      (first.length, car) :: encode(second)
    }
  }
  
  /**
   * P10
   */
  def encodeModified[T](list: List[T]): List[Any] = list match {
    case Nil => Nil
    case first :: second :: cdr if first != second =>
      first :: encodeModified(second :: cdr)
    case car :: cdr => {
      val (first, second) = list.span(x => x == car)
      (first.length, car) :: encodeModified(second)
    }
  }
  
  /**
   * P09
   */
  def pack[T](list: List[T]): List[List[T]] = list match {
    case Nil => Nil
    case car :: cdr => {
      val (first, second) = list.span(x => x == car)
      first :: pack(second)
    }
  }
  
  /**
   * P08
   */
  def compress[T](list: List[T]): List[T] = list match {
    case first :: second :: cdr if first == second => compress(first :: cdr)
    case car :: cdr => car :: compress(cdr)
    case Nil => Nil
  }
  
  /**
   * P07
   */
  def flatten(list: List[Any]): List[Any] = list.flatMap {
      case l: List[_] => flatten(l)
      case elm => List(elm)
  }
  
  /**
   * P06
   */
  def isParindrome[T](list: List[T]): Boolean = list == list.reverse
  
  /**
   * P05
   */
  def reverse[T](list: List[T]): List[T] =
    list.foldLeft(List.empty[T])((x, y) => y :: x)
  
  /**
   * P04
   */
  def length[T](list: List[T]): Int = list match {
    case Nil => 0;
    case car :: cdr => 1 + length(cdr)
  }
  
  /**
   * P03
   */
  def nth[T](n: Int, list: List[T]): T = (n, list) match {
    case (0, car :: cdr) => car
    case (m, car :: cdr) => nth(m-1, cdr)
    case _ => throw new Exception
  }
  
  /**
   * P02
   */
  def penultimate[T](list: List[T]): T = list match {
    case first :: second :: Nil => first
    case car :: cdr => last(cdr)
    case _ => throw new Exception
  }
  
  /**
   * P01
   */
  def last[T](list: List[T]): T = list match {
    case car :: Nil => car
    case car :: cdr => last(cdr)
    case _ => throw new Exception
  }
}
0