-
Notifications
You must be signed in to change notification settings - Fork 0
/
turn_game_1.c
78 lines (69 loc) · 1.15 KB
/
turn_game_1.c
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
#Turn-wise 2 player pickup game in which player 1 always gets to start and can pickup either 1 or 4 balls from a jar containing "n" number of balls.Given that player 1 and player 2 take turns and both play optimally.
Output "Yes" or "No" for wins and losses of Player 1.
#include<stdio.h>
int p;
int best(int n,int turn)
{
if(n==0 && turn==1)
{
return -10;
}
if(n==0 && turn==0)
{
return 10;
}
if(turn==1)
{
p=best(n-1,0);
if(n-4>=0 && p!=10)
{
p=best(n-4,0);
}
return p;
}
if(turn==0)
{
p=best(n-1,1);
if(n-4>=0 && p!=-10)
{
p=best(n-4,1);
}
return p;
}
}
void sumit(int k)
{
p=best(k-1,1);
if(k-4>=0 && p!=-10)
{
p=best(k-4,1);
}
if(p==-10)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
void main()
{
int t,c,i;
int n[100];
i=0;
printf("Enter number of test cases");
scanf("%d",&t);
c=t;
while (t-- >0)
{
printf ("Enter a number");
scanf("%d",&n[i]);
i++;
}
t=0;
while(t<=c)
{
sumit(n[t++]);
}
}