美文网首页
重拾算法Day07-小猫钓鱼

重拾算法Day07-小猫钓鱼

作者: 面试小集 | 来源:发表于2016-11-07 09:00 被阅读29次

小猫钓鱼游戏

#include <stdio.h>

struct queue {
    int data[1000];
    int head;
    int tail;
};

struct stack {
    int data[10];
    int top;
};

int main(int argc, const char * argv[]) {
    
    struct queue q1, q2;
    struct stack s;
    int book[10];
    
    q1.head = 1;
    q1.tail = 1;
    
    q2.head = 1;
    q2.tail = 1;
    
    s.top = 0;
    
    for (int i=0; i<=9; i++) {
        book[i] = 0;
    }
    
    for (int i=1; i<=6; i++) {
        scanf("%d", &q1.data[q1.tail]);
        q1.tail ++;
    }
    
    for (int i=1; i<=6; i++) {
        scanf("%d", &q2.data[q2.tail]);
        q2.tail ++;
    }
    
    
    while (q1.head<q1.tail && q2.head<q2.tail) {
        int t = q1.data[q1.head];
        
        if (book[t] == 0) {
            q1.head ++;
            s.top ++;
            s.data[s.top] = t;
            book[t] = 1;
        }else {
            q1.head ++;
            q1.data[q1.tail] = t;
            q1.tail ++;
            while (s.data[s.top] != t) {
                book[s.data[s.top]] = 0;
                q1.data[q1.tail] = s.data[s.top];
                q1.tail ++;
                s.top --;
            }
            book[s.data[s.top]] = 0;
            q1.data[q1.tail] = s.data[s.top];
            q1.tail ++;
            s.top --;
        }
        
        if (q1.head == q1.tail) {
            break;
        }
        
        
        t = q2.data[q2.head];
        
        if (book[t] == 0) {
            q2.head ++;
            s.top ++;
            s.data[s.top] = t;
            book[t] = 1;
        }else {
            q2.head ++;
            q2.data[q2.tail] = t;
            q2.tail ++;
            while (s.data[s.top] != t) {
                book[s.data[s.top]] = 0;
                q2.data[q2.tail] = s.data[s.top];
                q2.tail ++;
                s.top --;
            }
            book[s.data[s.top]] = 0;
            q2.data[q2.tail] = s.data[s.top];
            q2.tail ++;
            s.top --;
        }
        
        if (q2.head == q2.tail) {
            break;
        }
        
    }
    
    
    if (q2.head == q2.tail) {
        printf("heng\n");
        for (int i=q1.head; i<=q1.tail-1; i++) {
            printf("%d ", q1.data[i]);
        }
        if (s.top>0) {
            printf("桌上的牌是:");
            for (int i=1; i<s.top; i++) {
                printf("%d ", s.data[i]);
            }
        }
    }
    
    if (q1.head == q1.tail) {
        printf("ha\n");
        for (int i=q2.head; i<=q2.tail-1; i++) {
            printf("%d ", q2.data[i]);
        }
        if (s.top>0) {
            printf("桌上的牌是:");
            for (int i=1; i<s.top; i++) {
                printf("%d ", s.data[i]);
            }
        }
    }
    
    
    
    return 0;
}

相关文章

  • 重拾算法Day07-小猫钓鱼

    小猫钓鱼游戏

  • 小猫钓鱼的游戏算法

    目前是模拟是两个人对战的。使用栈和队列模拟。 #define kCardSize 52 typedef stru...

  • 小猫钓鱼🎣

    老猫和小猫一块儿在河边钓鱼。 一只蜻蜓飞来了。小猫看见了,放下钓鱼竿,就去捉蜻蜓。蜻蜓飞走了,小猫没捉着,空着手回...

  • 小猫钓鱼

    年纪一大把了,但我始终是那只钓鱼的小猫。无论年纪怎么增长,我永远无法学会像一只老猫那样钓鱼。 今天是星期天,没有应...

  • 小猫钓鱼

    僵尸打开了你的脑袋,失望地摇了摇头,走开了!旁边的屎壳郎眼睛亮了 我看你像书包( ꒪ д꒪ ⊂彡))Д´) 小猫钓...

  • 小猫钓鱼

    秋秋 2018-3-21

  • 小猫钓鱼

    亲子阅读第321天(3月5日) 今天晚上给崽崽讲了一个名叫《小猫钓鱼》的故事。故事讲的是一天,猫妈妈带着小...

  • 小猫钓鱼

    老师,今天我给你讲个故事,故事的名字叫小猫钓鱼。有一天嗯猫妈妈要带着小猫去河边钓鱼,猫妈妈嗯5:00就起来了,小猫...

  • 小猫钓鱼

    校区:科学创想机器人和平校区 时间:周日10:30-11:30 学员:王铂然 任教老师:杨玲 教学目标: 1.设计...

  • 小猫钓鱼。

    猫妈妈带着小猫去钓鱼。怎么做呀做,觉得好,无。聊。小花猫决定去捉蜻蜓。他来到蜜蜂窝。想吃蜂窝里的蜜汁。于是,小猫爬...

网友评论

      本文标题:重拾算法Day07-小猫钓鱼

      本文链接:https://www.haomeiwen.com/subject/gznnuttx.html