zeng1357 2021-11-18 17:16 采纳率: 0%
浏览 9
已结题

关于啊哈算法——纸牌游戏疑惑

阅读了啊哈算法中纸牌游戏部分,于是打算用c++实现,自己实现的感觉没问题,运行结果不同,各位帮忙看下问题所在。

img

img

img

img

img

#include <queue>
#include <stack>
#include <iostream>
#include <array>

using namespace std;

typedef queue<int> queInt;
typedef stack<int> staInt;
void CardGame(queInt& opposedA,queInt& opposedB)
{
    staInt sta;
    array<int,10> arr = { 0 };

    while (!opposedA.empty() && !opposedB.empty())
    {
        // A play a hand
        int iTem = opposedA.front();
        if (arr[iTem])
        {
            int ic = opposedA.front();
            opposedA.pop();
            opposedA.push(ic);
            for (int i = 0; i < sta.size();i++)
            {
                if (sta.top() == iTem)
                {
                    opposedA.push(sta.top());
                    arr[sta.top()] = 0;
                    sta.pop();
                    break;
                }
                else
                {
                    opposedA.push(sta.top());
                    arr[sta.top()] = 0;
                    sta.pop();
                    i--;
                }
            }

        }
        else
        {
            sta.push(opposedA.front());
            opposedA.pop();
            arr[iTem] = 1;
        }

        // B play a hand
        iTem = opposedB.front();
        if (arr[iTem])
        {
            int ic = opposedB.front();
            opposedB.pop();
            opposedB.push(ic);
            for (int i = 0; i < sta.size(); i++)
            {
                if (sta.top() == iTem)
                {
                    opposedB.push(sta.top());
                    arr[sta.top()] = 0;
                    sta.pop();
                    break;
                }
                else
                {
                    opposedB.push(sta.top());
                    arr[sta.top()] = 0;
                    sta.pop();
                    i--;
                }
            }
        }
        else
        {
            sta.push(opposedB.front());
            opposedB.pop();
            arr[iTem] = 1;
        }
    }
}

int main() 
{
    array<int, 6> arr1 = {2,4,1,2,5,6};
    array<int, 6> arr2 = { 3,1,3,5,6,4};
    //array<int, 3> arr1 = {1,2,3};
    //array<int, 3> arr2 = { 1,2,3 };
    queInt opposedA, opposedB;

    for each (int var1 in arr1)
    {
        opposedA.push(var1);
    }

    for each (int var2 in arr2)
    {
        opposedB.push(var2);
    }

    CardGame(opposedA, opposedB);

    if (opposedA.empty())
    {
        cout << " B win,Congratulation!" << endl;
        while (!opposedB.empty())
        {
            cout << opposedB.front() << " ";
            opposedB.pop();
        }
    }
    else
    {
        cout << " A win,Congratulation!" << endl;
        while (!opposedA.empty())
        {
            cout << opposedA.front() << " ";
            opposedA.pop();
        }
    }

    return 0;
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 11月26日
    • 创建了问题 11月18日

    悬赏问题

    • ¥15 安装quartus II18.1时弹出此error,怎么解决?
    • ¥15 keil官网下载psn序列号在哪
    • ¥15 想用adb命令做一个通话软件,播放录音
    • ¥30 Pytorch深度学习服务器跑不通问题解决?
    • ¥15 部分客户订单定位有误的问题
    • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
    • ¥15 Bug traq 数据包 大概什么价
    • ¥15 在anaconda上pytorch和paddle paddle下载报错
    • ¥25 自动填写QQ腾讯文档收集表
    • ¥15 DbVisualizer Pro 12.0.7 sql commander光标错位 显示位置与实际不符