标题:谜题问题,第二次输入后输出不对,请大佬指教。
只看楼主
x1471898145
Rank: 1
等 级:新手上路
帖 子:5
专家分:6
注 册:2018-5-27
结帖率:0
已结贴  问题点数:10 回复次数:4 
谜题问题,第二次输入后输出不对,请大佬指教。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>


int main()
{        
    while(1)
    {
        char str[5][5];
        char order[100];
        char c = '\0';
        int si = 0, sj = 0;
        memset(str, '\0', sizeof(str));
        memset(order, '\0', sizeof(order));
        int temp = 1;
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                scanf("%c", &str[i][j]);
                if (str[i][j] == ' ')
                {
                    si = i;
                    sj = j;
                }
                if (str[i][j] == 'Z')
                {
                    temp = 0;
                    break;
                }
            }
            if (!temp) break;
            c = getchar();           //读入,但是不用,  目的是跳过换行符
        }
        if (!temp) break;
        int l = -1;
        do
        {
            order[++l] = getchar();     //换行符也读入,后面再处理
        } while (order[l] != '0');
        /*    for (int i = 0; i < strlen(order); i++)
                printf("%c ", order[i]); */
        int bo = 1;
        for (int i = 0; i < l; i++)
        {
            if (order[i] == '\n') continue;
            switch (order[i])
            {
            case 'A': {
                if (si - 1 < 0) { bo = 0; break; }
                str[si][sj] = str[si - 1][sj];  str[si - 1][sj] = ' ';  si = si - 1;
            }; break;
            case 'B': {
                if (si + 1 > 4) { bo = 0; break; }
                str[si][sj] = str[si + 1][sj];  str[si + 1][sj] = ' ';  si = si + 1;
            }; break;
            case 'R': {
                if (sj + 1 > 4) { bo = 0; break; }
                str[si][sj] = str[si][sj + 1];  str[si][sj + 1] = ' ';  sj = sj + 1;
            }; break;
            case 'L': {
                if (sj - 1 < 0) { bo = 0; break; }
                str[si][sj] = str[si][sj - 1];  str[si][sj - 1] = ' ';  sj = sj - 1;
            }; break;
            default:printf("moves wrong\n"); break;
            }
            if (bo == 0) break;
        }
        if (bo == 1)
        {
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    printf("%c ", str[i][j]);
                }
                printf("\n");
            }
            printf("\n");
        }
        else printf("This puzzle has no final configuration\n");
        fflush(stdin);
    }
    system("pause");
}
搜索更多相关主题的帖子: str int order for break 
2018-10-27 11:41
Tomorrw_I
Rank: 10Rank: 10Rank: 10
等 级:禁止访问
威 望:6
帖 子:406
专家分:1712
注 册:2018-10-16
得分:5 
缺乏注释,看起来,好累啊! 看了两眼,应该是while  放的位置不对

学C有用吗?
学C++有前途吗?
数据库有必要学吗?
……
别问,你还什么都不会,学就对了
……
2018-10-27 11:45
no1xijin
Rank: 9Rank: 9Rank: 9
来 自:江西
等 级:贵宾
威 望:20
帖 子:211
专家分:1207
注 册:2015-7-8
得分:5 
简单说下:输入什么,预期结果是什么。

126邮箱联系方式:no1xijin@126. com
2018-10-27 13:37
x1471898145
Rank: 1
等 级:新手上路
帖 子:5
专家分:6
注 册:2018-5-27
得分:0 
是这个
2018-10-28 13:51
x1471898145
Rank: 1
等 级:新手上路
帖 子:5
专家分:6
注 册:2018-5-27
得分:0 
2018-10-28 13:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-490732-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.334925 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved