0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AtCoder B,C問題をKotlinで解こう - ABC259

Posted at

当記事ではAtCoder、ABCのB問題ならびにC問題(時々D問題も)のKotlinでの解法を超初心者向けに詳細に解説します。

B - Counterclockwise Rotation

やりたいこと

$(a,b)$ を原点を中心として反時計回りに $d$ 度回転させた場合、回転後の座標を $(x,y)$ とすると以下の式で求められる。

$x = cos(d)*a - sin(d)*b$
$y = sin(d)*a + cos(d)*b$

$cos$ ならびに $sin$ はKotlinで既に関数が用意されているので、それを当てはめれば良い。ただし関数にわたす角度はラジアンで扱う必要があるため、入力値で渡された角度は toRadians 関数でラジアンに変換しよう。

入力値の取得

    // 入力値の取得
    val (a, b, d) = readLine()!!.split(" ").map { it.toInt() }

角度の変換

    // 入力された角度をラジアンに変換
    val fixD = Math.toRadians(d.toDouble())

サンプルコード

main.kt
fun main(args: Array<String>) {
    // 入力値の取得
    val (a, b, d) = readLine()!!.split(" ").map { it.toInt() }

    // 入力された角度をラジアンに変換
    val fixD = Math.toRadians(d.toDouble())

    // 座標回転の式に当てはめる
    val newX = Math.cos(fixD) * a - Math.sin(fixD) * b
    val newY = Math.sin(fixD) * a + Math.cos(fixD) * b

    // 答えの出力
    println("$newX $newY")
}

C - XX to XXX

やりたいこと

文字列Tと文字列Sを先頭から順に文字ごとに比較していく。文字列Tのi番目の文字を t[i]、文字列Sのi番目の文字をs[i]として

  • t[i] == s[i] の場合
    • 何もしない
  • t[i] != s[i] の場合
    • t[i-2] == t[i-1] && t[i-1] == t[i] の場合
      • 文字列Sのi文字目に文字t[i]を挿入
    • 上記に該当しない場合
      • SをTに一致させることは不可能、処理終了

とする

入力値の取得

    // 入力値の取得
    val s = readLine()!!
    val t = readLine()!!

場合分け

文字ごとの比較は文字に対する操作の性質上、3文字目(index値の場合0から始まるので2以降)から行う。よって、文字列Tの長さが3未満の場合は操作が行えない。ただし、最初から文字列Sと文字列Tが一致している場合は操作は必要ない。そういった場合分けを実装しよう。

    // すでに一致している場合
    if (s == t) {
        return true
    }

    // Tの長さがSの長さ以下である場合(操作を行えない)
    if (t.length <= s.length) {
        return false
    }

    // 先頭の2文字は処理の対象外
    if (t.length < 2 || s.length < 2) {
        return false
    }
    if (s.take(2) != t.take(2)) {
        return false
    }
    if (t.length < 3) {
        return false
    }

サンプルコード

main.kt
fun main(args: Array<String>) {
    println(if (getAns()) "Yes" else "No")
}

private fun getAns(): Boolean {
    // 入力値の取得
    val s = readLine()!!
    val t = readLine()!!

    // すでに一致している場合
    if (s == t) {
        return true
    }

    // Tの長さがSの長さ以下である場合(操作を行えない)
    if (t.length <= s.length) {
        return false
    }

    // 先頭の2文字は処理の対象外
    if (t.length < 2 || s.length < 2) {
        return false
    }
    if (s.take(2) != t.take(2)) {
        return false
    }
    if (t.length < 3) {
        return false
    }

    // 文字挿入を行うため、文字列Sを文字のMutableListに変換する
    val tmp = s.toMutableList()
    for (i in (2..t.lastIndex)) {
        if (tmp.lastIndex >= i && tmp[i] == t[i]) {
            // t[i]とs[i]が一致している
            continue
        }
        if (t[i - 2] == t[i - 1] && t[i - 1] == t[i]) {
            // t[i]とs[i]が不一致だが操作を行える
            tmp.add(i, t[i])
        } else {
            // 操作を行えない
            return false
        }
    }
    return true
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?