鱼C论坛

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

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

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

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

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

x
#include<stdio.h>
struct queue{
        int data[1000];
        int head;
        int tail;
};
struct stack{
        int data[10];
        int top;
};
int main(){
        struct queue q1,q2;
        struct stack s;
        int book[10]={0};
        
        //初始化队列
        q1.head=0;q1.tail=0;
        q2.head=0;q2.tail=0;
        //初始化栈
        s.top=0;
        //手上的6张牌
        for(int i=0;i<6;i++){
                scanf("%d",&q1.data[q1.tail++]);
        }
        for(int i=0;i<6;i++){
                scanf("%d",&q1.data[q1.tail++]);
        }        
        //出牌
        int t;
        while(q1.head<q1.tail&&q2.head<q2.tail){
                t=q1.data[q1.head];        
                //打出牌 
                q1.head++;        
                //1没赢
                if(book[t]==0){
                                s.data[++s.top]=t;
                                book[t]=1;
                        }        
                //1可以赢
                else{
                        q1.data[q1.tail++]=t;
                                while(s.data[s.top]!=t){
                                        book[s.data[s.top]]=0;
                                        q1.data[q1.tail++]=s.data[s.top--];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
                                }
                        }        
                
                t=q2.data[q2.head];
                //打出牌                                                                
                    q2.head++;
                //2没赢                
                    if(book[t]==0){        
                        s.data[++s.top]=t;        
                            book[t]=1;
                    }
        //2可以赢
                    else{
                            q2.data[q2.tail++]=t;
                              while(s.data[s.top]!=t){
                                book[s.data[s.top]]=0;
                                        q2.data[q2.tail++]=s.data[s.top--];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
                            }
                    }                                                        
        }
        if(q2.head==q2.tail){
                        printf("q1赢了,他手上的牌是:");
                        for(int i=q1.head;i<q1.tail;i++){
                                printf(" %d",q1.data[i]);
                        }
                        if(s.top>0){
                                printf("\n桌上的数为:");
                                for(int i=1;i<=s.top;i++){
                                printf(" %d",s.data[i]);
                                }
                        }else{
                                printf("\n桌上无牌");
                        }
                }else{
                        printf("q2赢了,他手上的牌是:");
                        for(int i=q2.head;i<q2.tail;i++){
                                printf(" %d",q2.data[i]);
                        }
                        if(s.top>0){
                            printf("\n桌上的数为:");
                                for(int i=0;i<=s.top;i++){
                                                printf(" %d",s.data[i]);
                                }
                        }else{
                                printf("\n桌上无牌");
                        }                                                
                }                                
}
最佳答案
2023-11-7 19:16:25
这段代码中存在一些问题。首先,队列q2没有被正确初始化,其头尾指针应该也被初始化为0。其次,出牌的逻辑中存在错误,应该是先出牌再判断胜负,而不是先判断胜负再出牌。最后,打印桌上的牌时,应该从1开始遍历栈中的数据,而不是从0开始。

以下是修改后的代码:
#include<stdio.h>
struct queue{
    int data[1000];
    int head;
    int tail;
};
struct stack{
    int data[10];
    int top;
};
int main(){
    struct queue q1,q2;
    struct stack s;
    int book[10]={0};
    
    //初始化队列
    q1.head=0;q1.tail=0;
    q2.head=0;q2.tail=0;
    //初始化栈
    s.top=0;
    //手上的6张牌
    for(int i=0;i<6;i++){
        scanf("%d",&q1.data[q1.tail++]);
    }
    for(int i=0;i<6;i++){
        scanf("%d",&q2.data[q2.tail++]);
    }   
    //出牌
    int t;
    while(q1.head<q1.tail&&q2.head<q2.tail){
        t=q1.data[q1.head];
        q1.head++;
        if(book[t]==0){
            s.data[++s.top]=t;
            book[t]=1;
        }   
        else{
            q1.data[q1.tail++]=t;
            while(s.data[s.top]!=t){
                book[s.data[s.top]]=0;
                q1.data[q1.tail++]=s.data[s.top--];
            }
        } 
        t=q2.data[q2.head];
        q2.head++;
        if(book[t]==0){
            s.data[++s.top]=t;
            book[t]=1;
        }
        else{
            q2.data[q2.tail++]=t;
            while(s.data[s.top]!=t){
                book[s.data[s.top]]=0;
                q2.data[q2.tail++]=s.data[s.top--];
            }
        }
    }
    if(q2.head==q2.tail){
        printf("q1赢了,他手上的牌是:");
        for(int i=q1.head;i<q1.tail;i++){
            printf(" %d",q1.data[ i]);
        }
        if(s.top>0){
            printf("\n桌上的数为:");
            for(int i=1;i<=s.top;i++){
                printf(" %d",s.data[ i]);
            }
        }else{
            printf("\n桌上无牌");
        }
    }else{
        printf("q2赢了,他手上的牌是:");
        for(int i=q2.head;i<q2.tail;i++){
            printf(" %d",q2.data[ i]);
        }
        if(s.top>0){
            printf("\n桌上的数为:");
            for(int i=1;i<=s.top;i++){
                printf(" %d",s.data[ i]);
            }
        }else{
            printf("\n桌上无牌");
        }
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

以下是修改后的代码:
#include<stdio.h>
struct queue{
    int data[1000];
    int head;
    int tail;
};
struct stack{
    int data[10];
    int top;
};
int main(){
    struct queue q1,q2;
    struct stack s;
    int book[10]={0};
    
    //初始化队列
    q1.head=0;q1.tail=0;
    q2.head=0;q2.tail=0;
    //初始化栈
    s.top=0;
    //手上的6张牌
    for(int i=0;i<6;i++){
        scanf("%d",&q1.data[q1.tail++]);
    }
    for(int i=0;i<6;i++){
        scanf("%d",&q2.data[q2.tail++]);
    }   
    //出牌
    int t;
    while(q1.head<q1.tail&&q2.head<q2.tail){
        t=q1.data[q1.head];
        q1.head++;
        if(book[t]==0){
            s.data[++s.top]=t;
            book[t]=1;
        }   
        else{
            q1.data[q1.tail++]=t;
            while(s.data[s.top]!=t){
                book[s.data[s.top]]=0;
                q1.data[q1.tail++]=s.data[s.top--];
            }
        } 
        t=q2.data[q2.head];
        q2.head++;
        if(book[t]==0){
            s.data[++s.top]=t;
            book[t]=1;
        }
        else{
            q2.data[q2.tail++]=t;
            while(s.data[s.top]!=t){
                book[s.data[s.top]]=0;
                q2.data[q2.tail++]=s.data[s.top--];
            }
        }
    }
    if(q2.head==q2.tail){
        printf("q1赢了,他手上的牌是:");
        for(int i=q1.head;i<q1.tail;i++){
            printf(" %d",q1.data[ i]);
        }
        if(s.top>0){
            printf("\n桌上的数为:");
            for(int i=1;i<=s.top;i++){
                printf(" %d",s.data[ i]);
            }
        }else{
            printf("\n桌上无牌");
        }
    }else{
        printf("q2赢了,他手上的牌是:");
        for(int i=q2.head;i<q2.tail;i++){
            printf(" %d",q2.data[ i]);
        }
        if(s.top>0){
            printf("\n桌上的数为:");
            for(int i=1;i<=s.top;i++){
                printf(" %d",s.data[ i]);
            }
        }else{
            printf("\n桌上无牌");
        }
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-5 13:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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