Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Algorithms/kotlin/Game_Theory-Nimble_Game.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.*
import kotlin.collections.*
import kotlin.io.*
import kotlin.text.*

fun nimbleGame(s: Array<Int>): String {

var nim = 0

// 1. 돌의 개수가 홀수인 인덱스 값을 xor하여 균형상태인지 확인한다.
s.forEachIndexed { index, i ->
if ( i % 2 == 1) {
nim = nim.xor(index)
}
}

// 2. 균형상태라면 Second가 승리한다.
return if (nim == 0) "Second"
else "First"
}

fun main(args: Array<String>) {
val scan = Scanner(System.`in`)

val t = scan.nextLine().trim().toInt()

for (tItr in 1..t) {
val n = scan.nextLine().trim().toInt()

val s = scan.nextLine().split(" ").map{ it.trim().toInt() }.toTypedArray()

val result = nimbleGame(s)
println(result)
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ All about hackerrank
- Game_Theory-Game_of_Stones.kt
- Game_Theory-Introduction_to_Nim_Game.kt
- Game_Theory-Misère_Nim.kt
- Game_Theory-Nimble_Game.kt
- Game_Theory-Tower_Breakers.kt