AtCoder Beginners Selection

用 Kotlin 寫 Atcoder Beginners Selection

AtCoder Beginners Selection

用 AtCoder 的水題來練習 Kotlin 的基礎語法。

PracticeA - Welcome to AtCoder

 1private fun readLn() = readLine()!!
 2private fun readInt() = readLn().toInt()
 3private fun readStrings() = readLn().split(" ")
 4private fun readInts() = readStrings().map { it.toInt() }
 5
 6fun main() {
 7    val a = readInt()
 8    val(b, c) = readInts()
 9    val s = readLn()
10    println("${a + b + c} $s")
11}

ABC086A - Product

1private fun readLn() = readLine()!!
2private fun readInt() = readLn().toInt()
3private fun readStrings() = readLn().split(" ")
4private fun readInts() = readStrings().map { it.toInt() }
5
6fun main() {
7    val(a, b) = readInts()
8    println(if (a % 2 == 1 && b % 2 == 1 )  "Odd" else "Even")
9}

ABC081A - Placing Marbles

1private fun readLn() = readLine()!!
2fun countOne(s: String) = s.filter {it == '1'}.count()
3
4fun main() {
5    println(countOne(readLn()))
6}

ABC081B - Shift only

 1import kotlin.math.*
 2
 3private fun readLn() = readLine()!!
 4private fun readInt() = readLn().toInt()
 5private fun readStrings() = readLn().split(" ")
 6private fun readInts() = readStrings().map { it.toInt() }
 7
 8fun main() {
 9    val n = readInt()
10    val A = readInts()
11    var ans = A[0]
12    for (x in A) {
13        var cnt = 0
14        var tmp = x
15        while (tmp % 2 == 0) {
16            tmp /= 2
17            cnt++
18        }
19        ans = min(ans, cnt)
20    }
21    println(ans)
22}

ABC087B - Coins

 1private fun readLn() = readLine()!!
 2private fun readInt() = readLn().toInt()
 3
 4fun main() {
 5    val A = readInt()
 6    val B = readInt()
 7    val C = readInt()
 8    val X = readInt()
 9    var ans = 0
10    for (i in 0..A) {
11        for (j in 0..B) {
12            val sum = i * 500 + j * 100
13            if (sum <= X && (X - sum) <= C * 50)
14                ans++
15        }
16    }
17    println(ans)
18}

ABC083B - Some Sums

 1private fun readLn() = readLine()!!
 2private fun readInt() = readLn().toInt()
 3private fun readStrings() = readLn().split(" ")
 4private fun readInts() = readStrings().map { it.toInt() }
 5
 6fun main() {
 7    val(N, A, B) = readInts()
 8    var ans = 0
 9    for (i in 1..N) {
10        var tmp = i
11        var cnt = 0
12        while (tmp > 0) {
13            cnt += tmp % 10
14            tmp /= 10
15        }
16        if (cnt in A..B)
17            ans += i
18    }
19    println(ans)
20}

ABC088B - Card Game for Two

 1private fun readLn() = readLine()!!
 2private fun readInt() = readLn().toInt()
 3private fun readStrings() = readLn().split(" ")
 4private fun readInts() = readStrings().map { it.toInt() }
 5
 6fun main() {
 7    val N = readInt()
 8    var A = readInts()
 9    var cnt = IntArray(105) {0}
10    for (i in 0..N-1) cnt[A[i]]++
11    var ans = 0
12    var r = 1
13    for (i in 100 downTo 1) {
14        for (j in 1..cnt[i]) {
15            ans += r * i
16            r = -r
17        }
18    }
19    println(ans)
20}

ABC085B - Kagami Mochi

 1private fun readLn() = readLine()!!
 2private fun readInt() = readLn().toInt()
 3
 4fun main() {
 5    val N = readInt()
 6    var A = IntArray(101) {0}
 7    var ans = 0
 8    for (i in 1..N) {
 9        val x = readInt()
10        if (A[x] == 0) {
11            ans++
12            A[x] = 1
13        }
14    }
15    println(ans)
16}

ABC085C - Otoshidama

 1private fun readLn() = readLine()!!
 2private fun readInt() = readLn().toInt()
 3private fun readStrings() = readLn().split(" ")
 4private fun readInts() = readStrings().map { it.toInt() }
 5
 6fun main() {
 7    val(N, Y) = readInts()
 8    for (i in 0..N) {
 9        for (j in 0..(N - i)) {
10            val k = N - i - j
11            if (i * 10000 + j * 5000 + k * 1000 == Y) {
12                println("$i $j $k")
13                return
14            }
15        }
16    }
17    println("-1 -1 -1")
18}

ABC049C - Daydream

 1fun main() {
 2    val D = listOf("dream", "dreamer", "erase", "eraser")
 3    val DL = listOf(5, 7, 5, 6)
 4    val S = readLine()!!
 5    val L = S.length
 6    var DP = BooleanArray(L + 1) {false}
 7    DP[0] = true
 8    for (i in 1..L) {
 9        for (j in 0..3) {
10            val l = i - DL[j]
11            if (l < 0) continue
12            if (DP[l] && S.substring(l, i) == D[j]) {
13                DP[i] = true
14                break
15            }
16        }
17    }
18    println(if (DP[L]) "YES" else "NO")
19}

ABC086C - Traveling

 1import kotlin.math.abs
 2private fun readLn() = readLine()!!
 3private fun readInt() = readLn().toInt()
 4private fun readStrings() = readLn().split(" ")
 5private fun readInts() = readStrings().map { it.toInt() }
 6
 7fun main() {
 8    val N = readInt()
 9    var t = 0
10    var x = 0
11    var y = 0
12    for (i in 1..N) {
13        val(nt, nx, ny) = readInts()
14        val d = abs(nx - x) + abs(ny - y)
15        val dt = nt - t
16        if (d > dt || (dt - d) % 2 != 0) {
17            println("No")
18            return
19        }
20        t = nt
21        x = nx
22        y = ny
23    }
24    println("Yes")
25}
Built with Hugo
Theme Stack designed by Jimmy