鱼C论坛

 找回密码
 立即注册
查看: 2767|回复: 0

输入2 4 1 2 5 6 3 1 3 5 6 4时程序不对,调试发现s->top一直在减,哪里错了啊

[复制链接]
发表于 2022-3-20 12:05:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include<string.h>

  5. struct  queue//手牌
  6. {
  7.     int data[1000];
  8.     int head;
  9.     int tail;
  10. };

  11. struct stack//牌库
  12. {
  13.     int data[10];
  14.     int top;
  15. };

  16. int main()
  17. {
  18.     struct queue* q1=(struct queue*)malloc(sizeof(struct queue));
  19.     struct queue* q2=(struct queue*)malloc(sizeof(struct queue));
  20.     struct stack* s=(struct stack*)malloc(sizeof(struct stack));

  21.     q1->head=0;
  22.     q1->tail=0;
  23.     q2->head=0;
  24.     q2->tail=0;
  25.     s->top=-1;
  26.     //扑克牌拉大车
  27.     //队列记录1,2手中的牌,栈记录牌库

  28.     int i,t;
  29.     //往1,2 手中输送6张牌
  30.     for(i=0;i<6;i++)
  31.     {
  32.         scanf("%d",&q1->data[q1->tail]);
  33.         q1->tail++;
  34.     }
  35.     for(i=0;i<6;i++)
  36.     {
  37.         scanf("%d",&q2->data[q2->tail]);
  38.         q2->tail++;
  39.     }

  40.     int book[10]={0};//记录栈里每张牌地数量

  41.     while(q1->head<q1->tail && q2->head<q2->tail)
  42.     {
  43.         t=q1->data[q1->head];//储存1打出的牌
  44.         if(book[t]==0)//无相同牌的情况
  45.         {
  46.             book[t]=1;//数量+1
  47.             q1->head++;//打出
  48.             s->top++;
  49.             s->data[s->top]=t;//进入牌库
  50.             
  51.         }
  52.         else//有相同牌的情况
  53.         {
  54.             q1->head++;//打出
  55.             q1->data[q1->tail]=t;
  56.             q1->tail++;//回收
  57.             while(s->data[s->top]!=t)//回收牌库里面的牌
  58.             {
  59.                 q1->data[q1->tail]=s->data[s->top];
  60.                 q1->tail++;
  61.                 s->top--;
  62.             }
  63.             q1->data[q1->tail]=s->data[s->top];//回收那张相同的牌
  64.             q1->tail++;
  65.             s->top--;

  66.             book[t]=0;
  67.         }
  68.         if(q1->head==q1->tail)break;//1手牌没了,输了

  69.         t=q2->data[q2->head];
  70.         if(book[t]==0)
  71.         {
  72.             book[t]++;
  73.             s->top++;
  74.             s->data[s->top]=t;
  75.             q2->head++;
  76.         }
  77.         else
  78.         {
  79.             q2->head++;
  80.             q2->data[q2->tail]=t;
  81.             q2->tail++;
  82.             while(s->data[s->top]!=t)
  83.             {
  84.                 q2->data[q2->tail]=s->data[s->top];
  85.                 q2->tail++;
  86.                 s->top--;
  87.             }
  88.             q2->data[q2->tail]=s->data[s->top];
  89.             q2->tail++;
  90.             s->top--;
  91.             book[t]=0;
  92.             if(q2->head==q2->tail)break;
  93.         }
  94.     }
  95.     if(q1->head==q1->tail)
  96.     {
  97.         printf("1win");
  98.         // printf("2手上的牌是:");
  99.         // while(q2->head!=q2->tail)
  100.         // {
  101.         //     printf("%d ",q2->data[q2->head]);
  102.         //     q2->head++;
  103.         // }
  104.         // printf("桌上的牌是:");
  105.         // while(s->top>=0)
  106.         // {
  107.         //     printf("%d ",s->data[s->top]);
  108.         //     s->top--;
  109.         // }
  110.     }
  111.     else{
  112.         printf("2win");
  113.     }
  114.    
  115.     free(q1);
  116.     free(q2);
  117.     free(s);
  118.     return 0;
  119. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-12 01:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表