-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
99 lines (90 loc) · 2.67 KB
/
Program.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Linq;
namespace GameOfStones
{
class Program
{
static void Main(string[] args)
{
int t = Convert.ToInt32(Console.ReadLine());
for(int i = 0; i < t; i++)
{
int n = Convert.ToInt32(Console.ReadLine());
game(n);
}
}
static void game(int n)
{
Game game = new Game(n);
int optimal = Enum.GetValues(typeof(Moves))
.Cast<Int32>()
.Min() + Enum.GetValues(typeof(Moves))
.Cast<Int32>()
.Max();
while(game.p.All(i => !i.Lost))
{
for(int i = 0; i < 2; i++)
{
if (game.Stones >= Enum.GetValues(typeof(Moves)).Cast<Int32>().Min())
{
if (Enum.GetValues(typeof(Moves))
.Cast<Int32>()
.Any(x => x == game.Stones))
game.RemoveStones(Enum.GetValues(typeof(Moves))
.Cast<Int32>()
.Single(x => x == game.Stones));
else
game.RemoveStones(Enum.GetValues(typeof(Moves))
.Cast<Int32>()
.LastOrDefault(x => (game.Stones - x) % optimal <= 1));
}
else
{
game.p[i].Lost = true;
break;
}
}
}
Console.WriteLine(game.p[1].Lost ? "First" : "Second");
}
}
enum Moves
{
M1 = 2,
M2 = 3,
M3 = 5
}
class Game
{
int stones { get; set; }
public Player[] p = { new Player(), new Player() };
public Game(int stones)
{
if (stones >= Enum.GetValues(typeof(Moves)).Cast<Int32>().Min())
Stones = stones;
else p[0].Lost = true;
}
public int Stones
{
get { return stones; }
set { stones = value; }
}
public void RemoveStones(int n)
{
if (n > 0) stones -= n;
else RemoveStones(Enum.GetValues(typeof(Moves))
.Cast<Int32>()
.Where(x => x <= stones && !Enum.IsDefined(typeof(Moves), stones - x))
.Max());
}
}
class Player
{
bool lost { get; set; } = false;
public bool Lost
{
get { return lost; }
set { lost = value; }
}
}
}