扑克牌

来源:互联网 发布:券商工资知乎 编辑:程序博客网 时间:2024/05/17 03:42
★实验任务
一副扑克牌扣掉大小王后, 只剩下 52 张牌, 分两种颜色红 R 和黑 B, 每种颜色有 2、 3、
4、 5、 6、 7、 8、 9、 10、 J、 Q、 K、 A 十三种数值的牌各两张。 Alice 和 Bob 闲来无聊, 将
52 张牌分成了数量相等的两叠, 每人一叠, 轮流出牌(Alice 先出牌, 每次出牌只能拿最上
面的那一张),这样维护了一个出牌的序列。每次出牌,只要牌和序列中的某张牌同色且数
值相同, 就可以将这两张牌之间(包括这两张牌) 的所有牌全部放到自己的牌堆底下, 越晚出
的牌放越上面。没牌出的人算输。
★数据输入
输入为两行,每行26 张牌,每张牌由颜色和数值构成
第一行代表 Alice 的牌堆,输入的数据从左到右对应牌堆的从上到下
第二行代表 Bob 的牌堆
★数据输出
输出获胜者
输入示例                              输出示例
R2 R3 R4 R5 R6 R7 R8 R9               Alice
R10 RJ RQ RK RA R2 R3
R4 R5 R6 R7 R8 R9 R10
RJ RQ RK RA
B2 B3 B4 B5 B6 B7 B8 B9
B10 BJ BQ BK BA B2 B3

B4 B5 B6 B7 B8 B9 B10


#include<stdio.h>     #include<stdlib.h>     #include<string>     #define LEN sizeof(struct Node)     #define Max 26     struct Node     {         char sort[4];         struct Node *next;     };     typedef struct    {         Node *first;         Node *last;     }list,*linklist;     void creat(list &L)     {         L.first=(Node*)malloc(LEN);         Node *p1=NULL,*p2=L.first;         for(int i=0;i<Max;i++)         {             p1=(Node*)malloc(LEN);             scanf("%s",&p1->sort);             p2->next=p1;             p2=p1;         }         p2->next=NULL;         L.last=p2;     }     void game(list &L,list &sort)     {             Node *p=sort.first->next,*first=L.first->next;             for(;p&&strcmp(first->sort,p->sort);p=p->next);         L.first->next=first->next;             first->next=sort.first->next;                sort.first->next=first;             if(p)         {            L.last->next=sort.first->next;             L.last=p;               sort.first->next=p->next;            p->next=NULL;         }     }         int main()     {         list sort,L1,L2;             sort.first=sort.last=(Node*)malloc(LEN);             sort.first->next=NULL;             creat(L1); creat(L2);             for(int i=1;L1.first->next&&L2.first->next;i++)         {                 if(i&1) game(L1,sort);                 else    game(L2,sort);             }             if(L1.first->next==NULL)                 printf("Bob\n");             else                 printf("Alice\n");             return 0;         } 


0 0
原创粉丝点击