【问题标题】:scanf() taking more inputs than expected [closed]scanf() 接受比预期更多的输入[关闭]
【发布时间】:2021-01-22 16:36:24
【问题描述】:

我正在尝试检查徽标中的对称性问题(链接:https://www.hackerearth.com/practice/data-structures/arrays/multi-dimensional/practice-problems/algorithm/roy-and-symmetric-logos-1/description/)。这是接受名为 logo[][] 的数组输入的函数:

void take_Logo(int logo[N_Max][N_Max], int N)   //N is number of rows and columns
{
    int i = 0, j = 0;
    char in[80];
    for(i = 0; i<N; i++)
    {          
        for(j = 0; j<N; j++)
        {
            //gets(in);                                       //error in input here
            scanf("%s", in);
            logo[i][j] = in[j] - '0';
        }
    }
}

问题是当我运行代码时,对于 N = 2,它需要 4 个输入:

2
01
01
01
01

如何纠正?

我正在输入 01 作为二进制字符串。

完整代码如下:

#include<stdio.h>
#define N_Max 32
int check_Symm(int logo[N_Max][N_Max], int N);
void take_Logo(int logo[N_Max][N_Max], int N);

int main(void)
{
    int i = 0, T, N, logo[N_Max][N_Max];
    scanf("%d", &T);
    i = T;
    while(i > 0)
    {
        --i;
        scanf("%d", &N);
        take_Logo(logo,N);
        //check_Symm(logo, N) == 0 ? printf("NO\n") : printf("YES\n");
        for(int k = 0; k<N; k++)
        {
            for(int l = 0; l<N; l++)
            {
                printf("%d ", logo[k][l]);
            }
            printf("\n");
        }
    }
}

void take_Logo(int logo[N_Max][N_Max], int N)
{
    int i = 0, j = 0;
    char in[80];
    for(i = 0; i<N; i++)
    {          
        for(j = 0; j<N; j++)
        {
            //gets(in);                                       //error in input here
            scanf("%s", in);
            logo[i][j] = in[j] - '0';
        }
    }
}

int check_Symm(int logo[N_Max][N_Max], int N)
{
    int test = 1;
    if(N % 2 == 0)
    {
        for(int i = 0; i < N/2; i++)
        {
            for(int j = 0; j<N; j++)
            {
                if(logo[i][j] == logo[N-1-i][j])
                    continue;
                else
                {
                    test = 0;
                    return test;
                }
            }
        }
        for(int i = 0; i < N/2; i++)
        {
            for(int j = 0; j<N; j++)
            {
                if(logo[j][i] == logo[j][N-1-i])
                    continue;
                else
                {
                    test = 0;
                    return test;
                }
            }
        }
    }
    else
    {
        for(int i = 0; i < N/2; i++)
        {
            for(int j = 0; j<N; j++)
            {
                if(logo[i][j] == logo[N-1-i][j])
                    continue;
                else
                {
                    test = 0;
                    return test;
                }
            }
        }
        for(int i = 0; i < N/2; i++)
        {
            for(int j = 0; j<N; j++)
            {
                if(logo[j][i] == logo[j][N-1-i])
                    continue;
                else
                {
                    test = 0;
                    return test;
                }
            }
        }
        for(int i=0; i < N/2 ; i++)
        {
            if(logo[N/2][i] == logo[N/2][N-i-1])
                continue;
            else
            {
                test = 0;
                return test;
            }
        }
        for(int i=0; i < N/2 ; i++)
        {
            if(logo[i][N/2] == logo[N-i-1][N/2])
                continue;
            else
            {
                test = 0;
                return test;
            }
        }
    }
    return test;
}


【问题讨论】:

  • scanf("%s",...) 并不比gets 好。请参阅 Internet 上数十万个参考资料之一,了解为什么不应该使用 gets
  • 但是你展示的还不够。正在读取N 的代码在哪里?请发布一个完整的示例。
  • William 的“完整”示例是什么意思,我认为他们的意思是minimal reproducible example,这意味着您不应该发布大块。制作一个可以编译的最小示例。
  • @user3121023 谢谢。好像解决了问题
  • @klutt 抱歉,不知道。但是问题解决了。感谢您的建议!

标签: arrays c string


【解决方案1】:

问题是当我运行代码时,对于 N = 2,它需要 4 个输入:

这是N == 2的预期:

// Do loop twice
for(i = 0; i<N; i++) {          
    // Do loop twice
    for(j = 0; j<N; j++) {
        // Read 2 * 2 times
        scanf("%s", in);
        logo[i][j] = in[j] - '0';
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 2011-01-09
    • 1970-01-01
    相关资源
    最近更新 更多