熊猫钓鱼

题目描述
苗苗今天很无聊,就喊来希希和他一起玩熊猫钓鱼的扑克游戏。苗苗找来一堆扑克,将其平均分成两份,每人拿一份,苗苗先拿出手中的第一张牌放在桌子上,然后希希也拿出手中第一张牌放在苗苗刚打出来的牌上面,就像这样两人交替出牌。出牌时,如果谁打出的牌与桌上某张牌牌面相同,就可以把两张相同牌以及夹在其中的所有牌全部取走,并且把自己赢得的牌按照桌面上从上到下(1号牌压在2号牌上,那么1号牌就是在上,2号牌就在下)摆放的顺序依次放回手牌末尾。当任意一人手中的牌全部出完时,游戏结束,而另一个人获胜。

输入
第一行输入一个正整数N(N<=100),表示一开始每人手中有多少张牌(两个人一开始手中牌数相等),接下来两行,每行N个数字,分别表示一开始苗苗手中的牌和希希手中的牌,每张牌的牌面为数字1~13中某一个(保证在有限的回合后产生获胜者)。

输出
第一行输出产生获胜者的回合数。接下来一行,若最后苗苗获胜,输出“mm”,若希希获胜,则输出“xx”,接下来一行输出获胜者最后手中的牌。

样例
输入样例 1 复制

6
2 4 1 2 5 6
3 1 3 5 6 4
输出样例 1

25
xx
6 5 2 3 4 1

#include <stdio.h>
#include <stdlib.h>

#define _SQ_DEBUG_ 0
#define MM "mm"
#define XX "xx"

enum {
    ZER0 = 0,
    ONE = 1,
    THREE = 3,
    N = 100,
    MAXLEN = 220,
};

typedef struct TagFish {
    int cnt;
    int start;
    int end;
    int data[MAXLEN];
} Fish;

Fish g_mm = { 6, 0, 5, { 2, 4, 1, 2, 5, 6 } };
Fish g_xx = { 6, 0, 5, { 3, 1, 3, 5, 6, 4 } };

int g_fish[MAXLEN];
int g_cnt = 0;
int g_gameCnt = 0;

void FindFish(int curr, Fish *fish)
{
    int lastIdx = g_cnt - ONE;
    int idx = lastIdx;
    int findFlag = ZER0;
    for (; idx >= 0; --idx) {
        if (curr == g_fish[idx]) {
            findFlag = ONE;
            break;
        }
    }

    if (findFlag) {
        fish->end++;
        fish->end %= MAXLEN;
        fish->data[fish->end] = curr;
        fish->cnt++;
        g_cnt = idx;
        for (int i = lastIdx; i >= idx; --i) {
            fish->end++;
            fish->end %= MAXLEN;
            fish->data[fish->end] = g_fish[i];
            fish->cnt++;
        }
    } else {
        g_fish[g_cnt++] = curr;
    }
}

static inline void PrintFish(Fish *fish)
{
    if (fish->cnt <= 0) {
        return;
    }

    printf("%d", fish->data[fish->start]);
    for (int i = 1; i < fish->cnt; ++i) {
        printf(" %d", fish->data[(fish->start + i) % MAXLEN]);
    }
}

static inline void PrintGamePool()
{
    printf("-----GAMEPOOL------\n");
    for (int i = 0; i < g_cnt; ++i) {
        printf("%d ", g_fish[i]);
    }
    printf("\n-------------------\n");
    PrintFish(&g_mm);
    printf("\n");
    PrintFish(&g_xx);
    printf("\n");
}

void FishingProc()
{
    g_gameCnt = 0;
    g_cnt = 0;
    int curr;
    while (g_mm.cnt && g_xx.cnt) {
#if _SQ_DEBUG_
        printf("=========Round: %d========\n", g_gameCnt);
        PrintGamePool();
        printf("==========================\n");
#endif
        curr = g_mm.data[g_mm.start++];
        g_mm.start %= MAXLEN;
        g_mm.cnt--;
        FindFish(curr, &g_mm);

        curr = g_xx.data[g_xx.start++];
        g_xx.start %= MAXLEN;
        g_xx.cnt--;
        FindFish(curr, &g_xx);

        g_gameCnt++;
    }
#if _SQ_DEBUG_
    printf("=========Round: %d========\n", g_gameCnt);
    PrintGamePool();
    printf("==========================\n");
#endif

    printf("%d\n", g_gameCnt);
    if (g_mm.cnt == ZER0) {
        printf("%s\n", XX);
        PrintFish(&g_xx);
    } else {
        printf("%s\n", MM);
        PrintFish(&g_mm);
    }
}

int main()
{
    int n;
    int res = scanf_s("%d", &n, THREE);
    if (res <= ZER0) {
        return 0;
    }
    g_mm.start = g_xx.start = 0;
    g_mm.end = g_xx.end = n - 1;
    g_mm.cnt = g_xx.cnt = n;
    for (int i = 0; res >= 0 && i < n; ++i) {
        res = scanf_s("%d", &g_mm.data[i], THREE);
    }
    for (int i = 0; res >= 0 && i < n; ++i) {
        res = scanf_s("%d", &g_xx.data[i], THREE);
    }

    FishingProc();

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值