From 7618af64e2fbe2fc9d710215699e1837c01aec88 Mon Sep 17 00:00:00 2001 From: EthanShin Date: Thu, 4 Jul 2019 23:07:28 +0900 Subject: [PATCH 1/3] feat: Add Introduction_to_Nim_Game.kt description: Nim is the most famous two-player algorithm game. For each game, print the name of the winner on a new line. --- .../Game_Theory-Introduction_to_Nim_Game.kt | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Algorithms/kotlin/Game_Theory-Introduction_to_Nim_Game.kt diff --git a/Algorithms/kotlin/Game_Theory-Introduction_to_Nim_Game.kt b/Algorithms/kotlin/Game_Theory-Introduction_to_Nim_Game.kt new file mode 100644 index 0000000..d167cca --- /dev/null +++ b/Algorithms/kotlin/Game_Theory-Introduction_to_Nim_Game.kt @@ -0,0 +1,24 @@ +import java.util.* +import kotlin.collections.* +import kotlin.io.* +import kotlin.text.* + +fun nimGame(pile: Array): String { + + return "First" +} + +fun main(args: Array) { + val scan = Scanner(System.`in`) + + val g = scan.nextLine().trim().toInt() + + for (gItr in 1..g) { + val n = scan.nextLine().trim().toInt() + + val pile = scan.nextLine().split(" ").map{ it.trim().toInt() }.toTypedArray() + + val result = nimGame(pile) + println(result) + } +} \ No newline at end of file From f5a981c3049d4b468384c20d3beaf129422ca974 Mon Sep 17 00:00:00 2001 From: EthanShin Date: Thu, 4 Jul 2019 23:35:14 +0900 Subject: [PATCH 2/3] feat: Modify Introduction_to_Nim_Game.kt description: Nim is the most famous two-player algorithm game. For each game, print the name of the winner on a new line. --- .../kotlin/Game_Theory-Introduction_to_Nim_Game.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Algorithms/kotlin/Game_Theory-Introduction_to_Nim_Game.kt b/Algorithms/kotlin/Game_Theory-Introduction_to_Nim_Game.kt index d167cca..2c811b6 100644 --- a/Algorithms/kotlin/Game_Theory-Introduction_to_Nim_Game.kt +++ b/Algorithms/kotlin/Game_Theory-Introduction_to_Nim_Game.kt @@ -5,7 +5,14 @@ import kotlin.text.* fun nimGame(pile: Array): String { - return "First" + // 1. Nim 게임의 공략법인 균형상태를 확인한다. + // 2. pile의 요소를 모두 xor하여 값이 0이 나오면 균형상태이므로, Second가 승리한다. + pile.reduce { acc, i -> acc xor i }.let { + return when(it) { + 0 -> "Second" + else -> "First" + } + } } fun main(args: Array) { From d618189e65001b3ae5bdab2bbdea2fd4f9483dfb Mon Sep 17 00:00:00 2001 From: EthanShin Date: Thu, 4 Jul 2019 23:38:25 +0900 Subject: [PATCH 3/3] docs: Modify REMOTE_README.md resolved: #112 description: Add a file name to the index. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c96db03..a57f9c7 100644 --- a/README.md +++ b/README.md @@ -85,4 +85,5 @@ All about hackerrank - Bit_Manipulation-Sum_vs_XOR.kt - Game_Theory-A_Chessboard_Game.kt - Game_Theory-Game_of_Stones.kt +- Game_Theory-Introduction_to_Nim_Game.kt - Game_Theory-Tower_Breakers.kt \ No newline at end of file