-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathNGM.cs
47 lines (42 loc) · 1.5 KB
/
NGM.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
// https://www.spoj.com/problems/NGM/ #game
// For a game between two players where digits are subtracted from a number
// in turn, finds who wins the game and what player one's first move should be if he can win.
public static class NGM
{
// If the number doesn't end in zero then the first player guarantees
// victory by subtracting the final digit to make a number that does end in zero.
// Then the second player has to subtract less than 10 from that number, so the
// number after his turn definitely doesn't end in zero and the cycle repeats.
// The reverse happens if the number starts out ending in zero.
// For example, here are the moves for number = 1234567891:
// =P1=> 1234567890
// =P2=> 123456788x
// =P1=> 1234576880
// ...
// =P2=> 27
// =P1=> 20
// =P2=> 18
// =P1=> 10
// =P2=> 9
// =P1=> 0 (P1 wins)
public static Tuple<int, int?> Solve(int number)
{
bool playerOneWins = number % 10 != 0;
int winner = playerOneWins ? 1 : 2;
int? winningFirstMoveForPlayerOne = playerOneWins ? number % 10 : (int?)null;
return Tuple.Create(winner, winningFirstMoveForPlayerOne);
}
}
public static class Program
{
private static void Main()
{
Tuple<int, int?> gameResults = NGM.Solve(int.Parse(Console.ReadLine()));
Console.WriteLine(gameResults.Item1);
if (gameResults.Item2.HasValue)
{
Console.WriteLine(gameResults.Item2);
}
}
}