2019年3月31日日曜日

開発環境

Head First Kotlin: A Brain-Friendly Guide (Dawn Griffiths(著)、David Griffiths(著)、O'Reilly Media)のChapter 3(functions - Getting Out of Main)、Mixed Messages(79)の解答を求めてみる。

コード

fun main() {
    var x = 0
    var y = 20
    for(outer in 1..3) {
        for (inner in 4 downTo 2) {
            x += 6
            y++
            x += 3
        }
        y -= 2
    }
    println("${x == 81}, ${y == 23}")

    x = 0
    y = 20
    for (outer in 1..3) {
        for (inner in 4 downTo 2) {
            x--
            y++
            x += 3
        }
        y -= 2
    }
    println("${x == 18}, ${y == 23}")

    x = 0
    y = 20
    for (outer in 1..3) {
        for (inner in 4 downTo 2) {
            y = x + y
            y++
            x += 3
        }
        y -= 2
    }
    println("${x == 27}, ${y == 131}")

    x = 0
    y = 20
    for (outer in 1..3) {
        for (inner in 4 downTo 2) {
            y = 7
            y++
            x += 3
        }
        y -= 2
    }
    println("${x == 27}, ${y == 6}")

    x = 0
    y = 20
    for (outer in 1..3) {
        for (inner in 4 downTo 2) {
            x = x + y
            y = x - 7
            y++
            x += 3
            // outer=1
            // inner=4: x=23, y=14
            // inner=3: x=40, y=31
            // inner=2: x=74, y=65
            // inner=1: x=142, y=133

            // outer=2
            // inner=4: x=276, y=267
        }
        y -= 2
        // outer=1: x=142, y=131

    }
    // y - x = 11
    println("${x == 4286}, ${y == 4275}")

    x = 0
    y = 20
    for (outer in 1..3) {
        for (inner in 4 downTo 2) {
            x = y
            y++
            y++
            x += 3
        }
        y -= 2
    }
    // x - y = 3
    println("${x == 35}, ${y == 32}")
}

入出力結果

true, true
true, true
true, true
true, true
true, true
true, true

Process finished with exit code 0

0 コメント:

コメントを投稿

関連コンテンツ