- #include < iostream > #include < vector > #include < iostream > using namespace std;
- struct myqueue {
- int date[100];
- int top;
- int tail;
- myqueue() {
- top = -1;
- tail = -1;
- }
- int myempty() {
- return top == tail;
- }
- int mytop() {
- if (myempty()) return 0;
- return date[top];
- }
- void myinsert(int x) {
- if (top == -1) {
- date[++top] = x;
- tail = 1;
- } else {
- date[tail] = x;
- tail++;
- }
- }
- void mypop() {
- if (top < tail) top++;
- else {
- cout << "空队列" << endl;
- }
- }
- void print() {
- for (int i = top; i < tail; i++) cout << date[i] << " ";
- cout << endl;
- }
- };
- struct mystack {
- int date[100];
- int top;
- mystack() {
- top = -1;
- }
- int mytop() {
- if (top == -1) return 0;
- return date[top];
- }
- void mypush(int x) {
- date[++top] = x;
- }
- void mypop() {
- if (top != -1) top--;
- }
- int findx(int x) {
- int ok = -1;
- for (int i = top - 1; i >= 0; i--) if (date[i] == x) {
- ok = i;
- break;
- }
- return ok;
- }
- void popok(int x, myqueue & y) {
- for (int i = top; i >= x; i--) y.myinsert(date[i]);
- top = x - 1;
- }
- void print() {
- for (int i = 0; i <= top; i++) cout << date[i] << " ";
- cout << endl;
- }
- };
- int main() {
- myqueue x1,
- x2;
- mystack x;
- int a[6] = {
- 2,
- 4,
- 1,
- 2,
- 5,
- 6
- };
- int b[6] = {
- 3,
- 1,
- 3,
- 5,
- 6,
- 4
- };
- for (int i = 0; i < 6; i++) {
- x1.myinsert(a[i]);
- x2.myinsert(b[i]);
- }
- while (!x1.myempty() && !x2.myempty()) {
- x.mypush(x1.mytop());
- int t = x1.mytop();
- x1.mypop();
- if (x.findx(t) >= 0) {
- x.popok(x.findx(t), x1);
- }
- if (x1.myempty()) break;
- x.mypush(x2.mytop());
- int m = x2.mytop();
- x2.mypop();
- if (x.findx(m) >= 0) {
- x.popok(x.findx(m), x2);
- }
- }
- if (x1.myempty()) {
- cout << "小婷 Win" << endl;
- cout << "小婷手中牌为:";
- x2.print();
- cout << "桌子上牌为:";
- x.print();
- } else {
- cout << "小白 Win" << endl;
- cout << "小白手中牌为:";
- x1.print();
- cout << "桌子上牌为:";
- x.print();
- }
- return 0;
- }
来源: http://www.bubuko.com/infodetail-2222010.html