-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_game_21BCE3982.cpp
78 lines (59 loc) · 1.45 KB
/
stack_game_21BCE3982.cpp
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
#include <iostream>
using namespace std;
bool isEmpty(int top){
return top==-1;
}
int pop(int stack[],int &top){
int x=stack[top];
top--;
return x;
}
void push(int stack[],int &top, int element){
stack[top+1]=element;
top++;
}
int main(){
int n;
cout<<"Enter number of elements in stack : "<<endl;
cin>>n;
int stack1[1000]={0};int stack2[1000]={0}; //space for pushing
int top1=-1;
int top2=-1;
cout<<"Enter elements of stacks : "<<endl;
for (int i = 0; i < n;i++){
cin>>stack1[i];
cin>>stack2[i];
top1++;
top2++;
}
while (!isEmpty(top1)&&!isEmpty(top2)){
int n1=pop(stack1,top1);
int n2=pop(stack2,top2);
if (n1>n2){
int diff=n1-n2;
int num_elements=top1+1;
if (diff>=num_elements) {
top1=-1;
break;
}
for (int i=0;i<diff;i++){
int x=pop(stack1,top1);
push(stack2,top2,x);
}
}
if (n1<n2){
int diff=n2-n1;
int num_elements=top2+1;
if(diff>=num_elements){
top2=-1;
break;
}
for (int i=0;i<diff;i++){
int x=pop(stack2,top2);
push(stack1,top1,x);
}
}
}
if (isEmpty(top1)) cout<<"Player 1 has won";
else cout<<"Player 2 has won";
}