結果

問題 No.254 文字列の構成
ユーザー くわいくわい
提出日時 2015-09-26 21:28:26
言語 Scala(Beta)
(3.4.0)
結果
AC  
実行時間 968 ms / 5,000 ms
コード長 1,435 bytes
コンパイル時間 10,086 ms
コンパイル使用メモリ 268,272 KB
実行使用メモリ 65,204 KB
最終ジャッジ日時 2024-04-30 13:47:32
合計ジャッジ時間 43,215 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 939 ms
64,872 KB
testcase_01 AC 941 ms
65,056 KB
testcase_02 AC 947 ms
65,152 KB
testcase_03 AC 943 ms
64,928 KB
testcase_04 AC 944 ms
65,080 KB
testcase_05 AC 947 ms
65,112 KB
testcase_06 AC 942 ms
64,888 KB
testcase_07 AC 947 ms
64,980 KB
testcase_08 AC 948 ms
64,840 KB
testcase_09 AC 931 ms
65,108 KB
testcase_10 AC 938 ms
64,924 KB
testcase_11 AC 935 ms
65,156 KB
testcase_12 AC 943 ms
64,900 KB
testcase_13 AC 946 ms
65,032 KB
testcase_14 AC 953 ms
64,920 KB
testcase_15 AC 948 ms
64,992 KB
testcase_16 AC 961 ms
64,908 KB
testcase_17 AC 955 ms
64,844 KB
testcase_18 AC 959 ms
65,204 KB
testcase_19 AC 953 ms
65,168 KB
testcase_20 AC 960 ms
64,876 KB
testcase_21 AC 968 ms
65,032 KB
testcase_22 AC 957 ms
64,896 KB
testcase_23 AC 960 ms
64,932 KB
testcase_24 AC 964 ms
64,892 KB
testcase_25 AC 954 ms
64,904 KB
testcase_26 AC 940 ms
64,876 KB
testcase_27 AC 944 ms
64,996 KB
testcase_28 AC 940 ms
65,052 KB
testcase_29 AC 948 ms
64,956 KB
testcase_30 AC 953 ms
64,872 KB
testcase_31 AC 956 ms
65,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner

object Problem254 {

  // 好きな寿司ネタは寒ブリ
  def main(args: Array[String]) = {
    val sc = new Scanner(System.in)
    val n = sc.nextInt

    val sb = new StringBuilder
    var remain = n
    var charIndex = 0
    while (remain > 0) {
      val len = binarySearch(ABPalindrome, remain, 1, 65000)
      remain -= ABPalindrome(len)

      val subPalindrome = getABString(len, charIndex)
      sb.append(subPalindrome)
      charIndex += 2
    }
    println(sb.toString)
  }

  /*
  len以下の(偶数 or 奇数)の和
  6 abab 4 2
  9 ababa 5 3 1
  12 ababab 6 4 2
  16 abababa 7 5 3 1
  20 abababab 8 6 4 2
  25 ababababa 9 7 5 3 1
  */
  def ABPalindrome(n: Int): Int = {
    n % 2 match {
      case 0 => (n / 2) * (n / 2 + 1)
      case 1 => Math.pow((n / 2 + 1), 2).toInt
    }
  }

  def binarySearch(f: Int => Int, n: Int, low: Int, high: Int): Int = {
    val mid = (low + high) / 2

    if (low > high) {
      return mid
    }

    (f(mid), n) match {
      case (x, y) if x == y => mid
      case (x, y) if x < y => binarySearch(f, n, mid + 1, high)
      case _ => binarySearch(f, n, low, mid - 1)
    }
  }

  def getABString(len: Int, charIndex: Int): String = {
    val a = ('a' + charIndex).toChar
    val b = ('b' + charIndex).toChar
    val ab = a.toString + b.toString

    len % 2 match {
      case 0 => ab * (len / 2)
      case 1 => ab * (len / 2) + a
    }
  }

}
0