鱼C论坛

 找回密码
 立即注册
查看: 1238|回复: 2

[已解决]小猫钓鱼案例输出有问题

[复制链接]
发表于 2023-11-7 19:16:09 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>
  2. struct queue{
  3.         int data[1000];
  4.         int head;
  5.         int tail;
  6. };
  7. struct stack{
  8.         int data[10];
  9.         int top;
  10. };
  11. int main(){
  12.         struct queue q1,q2;
  13.         struct stack s;
  14.         int book[10]={0};
  15.        
  16.         //初始化队列
  17.         q1.head=0;q1.tail=0;
  18.         q2.head=0;q2.tail=0;
  19.         //初始化栈
  20.         s.top=0;
  21.         //手上的6张牌
  22.         for(int i=0;i<6;i++){
  23.                 scanf("%d",&q1.data[q1.tail++]);
  24.         }
  25.         for(int i=0;i<6;i++){
  26.                 scanf("%d",&q1.data[q1.tail++]);
  27.         }       
  28.         //出牌
  29.         int t;
  30.         while(q1.head<q1.tail&&q2.head<q2.tail){
  31.                 t=q1.data[q1.head];       
  32.                 //打出牌
  33.                 q1.head++;       
  34.                 //1没赢
  35.                 if(book[t]==0){
  36.                                 s.data[++s.top]=t;
  37.                                 book[t]=1;
  38.                         }       
  39.                 //1可以赢
  40.                 else{
  41.                         q1.data[q1.tail++]=t;
  42.                                 while(s.data[s.top]!=t){
  43.                                         book[s.data[s.top]]=0;
  44.                                         q1.data[q1.tail++]=s.data[s.top--];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
  45.                                 }
  46.                         }       
  47.                
  48.                 t=q2.data[q2.head];
  49.                 //打出牌                                                               
  50.                     q2.head++;
  51.                 //2没赢               
  52.                     if(book[t]==0){       
  53.                         s.data[++s.top]=t;       
  54.                             book[t]=1;
  55.                     }
  56.         //2可以赢
  57.                     else{
  58.                             q2.data[q2.tail++]=t;
  59.                               while(s.data[s.top]!=t){
  60.                                 book[s.data[s.top]]=0;
  61.                                         q2.data[q2.tail++]=s.data[s.top--];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
  62.                             }
  63.                     }                                                       
  64.         }
  65.         if(q2.head==q2.tail){
  66.                         printf("q1赢了,他手上的牌是:");
  67.                         for(int i=q1.head;i<q1.tail;i++){
  68.                                 printf(" %d",q1.data[i]);
  69.                         }
  70.                         if(s.top>0){
  71.                                 printf("\n桌上的数为:");
  72.                                 for(int i=1;i<=s.top;i++){
  73.                                 printf(" %d",s.data[i]);
  74.                                 }
  75.                         }else{
  76.                                 printf("\n桌上无牌");
  77.                         }
  78.                 }else{
  79.                         printf("q2赢了,他手上的牌是:");
  80.                         for(int i=q2.head;i<q2.tail;i++){
  81.                                 printf(" %d",q2.data[i]);
  82.                         }
  83.                         if(s.top>0){
  84.                             printf("\n桌上的数为:");
  85.                                 for(int i=0;i<=s.top;i++){
  86.                                                 printf(" %d",s.data[i]);
  87.                                 }
  88.                         }else{
  89.                                 printf("\n桌上无牌");
  90.                         }                                               
  91.                 }                               
  92. }
复制代码
最佳答案
2023-11-7 19:16:25
这段代码中存在一些问题。首先,队列q2没有被正确初始化,其头尾指针应该也被初始化为0。其次,出牌的逻辑中存在错误,应该是先出牌再判断胜负,而不是先判断胜负再出牌。最后,打印桌上的牌时,应该从1开始遍历栈中的数据,而不是从0开始。

以下是修改后的代码:

  1. #include<stdio.h>
  2. struct queue{
  3.     int data[1000];
  4.     int head;
  5.     int tail;
  6. };
  7. struct stack{
  8.     int data[10];
  9.     int top;
  10. };
  11. int main(){
  12.     struct queue q1,q2;
  13.     struct stack s;
  14.     int book[10]={0};
  15.    
  16.     //初始化队列
  17.     q1.head=0;q1.tail=0;
  18.     q2.head=0;q2.tail=0;
  19.     //初始化栈
  20.     s.top=0;
  21.     //手上的6张牌
  22.     for(int i=0;i<6;i++){
  23.         scanf("%d",&q1.data[q1.tail++]);
  24.     }
  25.     for(int i=0;i<6;i++){
  26.         scanf("%d",&q2.data[q2.tail++]);
  27.     }   
  28.     //出牌
  29.     int t;
  30.     while(q1.head<q1.tail&&q2.head<q2.tail){
  31.         t=q1.data[q1.head];
  32.         q1.head++;
  33.         if(book[t]==0){
  34.             s.data[++s.top]=t;
  35.             book[t]=1;
  36.         }   
  37.         else{
  38.             q1.data[q1.tail++]=t;
  39.             while(s.data[s.top]!=t){
  40.                 book[s.data[s.top]]=0;
  41.                 q1.data[q1.tail++]=s.data[s.top--];
  42.             }
  43.         }
  44.         t=q2.data[q2.head];
  45.         q2.head++;
  46.         if(book[t]==0){
  47.             s.data[++s.top]=t;
  48.             book[t]=1;
  49.         }
  50.         else{
  51.             q2.data[q2.tail++]=t;
  52.             while(s.data[s.top]!=t){
  53.                 book[s.data[s.top]]=0;
  54.                 q2.data[q2.tail++]=s.data[s.top--];
  55.             }
  56.         }
  57.     }
  58.     if(q2.head==q2.tail){
  59.         printf("q1赢了,他手上的牌是:");
  60.         for(int i=q1.head;i<q1.tail;i++){
  61.             printf(" %d",q1.data[ i]);
  62.         }
  63.         if(s.top>0){
  64.             printf("\n桌上的数为:");
  65.             for(int i=1;i<=s.top;i++){
  66.                 printf(" %d",s.data[ i]);
  67.             }
  68.         }else{
  69.             printf("\n桌上无牌");
  70.         }
  71.     }else{
  72.         printf("q2赢了,他手上的牌是:");
  73.         for(int i=q2.head;i<q2.tail;i++){
  74.             printf(" %d",q2.data[ i]);
  75.         }
  76.         if(s.top>0){
  77.             printf("\n桌上的数为:");
  78.             for(int i=1;i<=s.top;i++){
  79.                 printf(" %d",s.data[ i]);
  80.             }
  81.         }else{
  82.             printf("\n桌上无牌");
  83.         }
  84.     }
  85. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-7 19:16:25 | 显示全部楼层    本楼为最佳答案   
这段代码中存在一些问题。首先,队列q2没有被正确初始化,其头尾指针应该也被初始化为0。其次,出牌的逻辑中存在错误,应该是先出牌再判断胜负,而不是先判断胜负再出牌。最后,打印桌上的牌时,应该从1开始遍历栈中的数据,而不是从0开始。

以下是修改后的代码:

  1. #include<stdio.h>
  2. struct queue{
  3.     int data[1000];
  4.     int head;
  5.     int tail;
  6. };
  7. struct stack{
  8.     int data[10];
  9.     int top;
  10. };
  11. int main(){
  12.     struct queue q1,q2;
  13.     struct stack s;
  14.     int book[10]={0};
  15.    
  16.     //初始化队列
  17.     q1.head=0;q1.tail=0;
  18.     q2.head=0;q2.tail=0;
  19.     //初始化栈
  20.     s.top=0;
  21.     //手上的6张牌
  22.     for(int i=0;i<6;i++){
  23.         scanf("%d",&q1.data[q1.tail++]);
  24.     }
  25.     for(int i=0;i<6;i++){
  26.         scanf("%d",&q2.data[q2.tail++]);
  27.     }   
  28.     //出牌
  29.     int t;
  30.     while(q1.head<q1.tail&&q2.head<q2.tail){
  31.         t=q1.data[q1.head];
  32.         q1.head++;
  33.         if(book[t]==0){
  34.             s.data[++s.top]=t;
  35.             book[t]=1;
  36.         }   
  37.         else{
  38.             q1.data[q1.tail++]=t;
  39.             while(s.data[s.top]!=t){
  40.                 book[s.data[s.top]]=0;
  41.                 q1.data[q1.tail++]=s.data[s.top--];
  42.             }
  43.         }
  44.         t=q2.data[q2.head];
  45.         q2.head++;
  46.         if(book[t]==0){
  47.             s.data[++s.top]=t;
  48.             book[t]=1;
  49.         }
  50.         else{
  51.             q2.data[q2.tail++]=t;
  52.             while(s.data[s.top]!=t){
  53.                 book[s.data[s.top]]=0;
  54.                 q2.data[q2.tail++]=s.data[s.top--];
  55.             }
  56.         }
  57.     }
  58.     if(q2.head==q2.tail){
  59.         printf("q1赢了,他手上的牌是:");
  60.         for(int i=q1.head;i<q1.tail;i++){
  61.             printf(" %d",q1.data[ i]);
  62.         }
  63.         if(s.top>0){
  64.             printf("\n桌上的数为:");
  65.             for(int i=1;i<=s.top;i++){
  66.                 printf(" %d",s.data[ i]);
  67.             }
  68.         }else{
  69.             printf("\n桌上无牌");
  70.         }
  71.     }else{
  72.         printf("q2赢了,他手上的牌是:");
  73.         for(int i=q2.head;i<q2.tail;i++){
  74.             printf(" %d",q2.data[ i]);
  75.         }
  76.         if(s.top>0){
  77.             printf("\n桌上的数为:");
  78.             for(int i=1;i<=s.top;i++){
  79.                 printf(" %d",s.data[ i]);
  80.             }
  81.         }else{
  82.             printf("\n桌上无牌");
  83.         }
  84.     }
  85. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-7 19:17:45 | 显示全部楼层
你好,感谢您分享代码。请问您遇到了什么问题,需要我帮您解决吗?

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-21 14:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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