【问题标题】:Wrong scanf in 2D array二维数组中的错误scanf
【发布时间】:2016-04-10 18:05:34
【问题描述】:

我有以下代码:

#include <stdio.h>

int main() {

int tc,T;
scanf("%d", &T);

for(tc=0; tc<T; tc++){
    int flag=0;
    int R,C;

    scanf("%d %d", &R, &C);
    {
        int i,j;
        int r,c;
        int Grid[R][C];

        //Read the Grid
        for(i=0; i<R; i++){
            for(j=0; j<C; j++){
                scanf("%d", &Grid[i,j]);
            }
        }

        scanf("%d %d", &r, &c);
        {
            int Pattern[r][c];

            //Read the Grid
            for(i=0; i<r; i++){
                for(j=0; j<c; j++){
                    scanf("%d", &Pattern[i,j]);
                }
            }
            //Here we have both the Grid and the Pattern
            for(i=0; i<R; i++){
                for(j=0; j<C; j++){
                    if(Grid[i,j]==Pattern[0,0] && ((i+r)<=R) && ((j+c)<=C)){
                        int x,y;
                        int innerFlag=1;

                        for(x=0; x<r; x++){
                            for(y=0; y<c; y++){
                                if(Grid[x+i,y+j]!=Pattern[x,y]) innerFlag=0;
                            }
                        }

                        if(innerFlag == 1){ 
                            //Set the flag to 1 and break out of the nested loops
                            flag=1;
                            j=C;
                            i=R;
                        }
                    }
                }
            }

            //Here all the calculation is done and the flag is set
            if(flag==1) printf("YES\n");
            else printf("NO\n");
        }

    }
}

return 0;
}

这给了我以下错误:

  1. 警告:格式“%d”需要“int ”类型的参数,但参数 2 的类型为“int ()[(sizetype)(C)]” scanf("%d", &Grid[i,j]);
  2. 警告:格式“%d”需要“int ”类型的参数,但参数 2 的类型为“int ()[(sizetype)(c)]” scanf("%d", &Pattern[i,j]);

你知道这段代码有什么问题吗?

P.S 我还检查了 Grid 和 Pattern,它显示为垃圾!

【问题讨论】:

  • 网格/模式的代码? [] 是否为任一类重载以将两个整数作为参数?
  • @mwm314 题目标记为C,代码貌似是C,所以没有重载。
  • Grid[i,j] 中的逗号被解释为comma operator,因此Grid[i,j]Grid[j] 相同。

标签: c scanf


【解决方案1】:

在 C 中,您应该像这样为数组下标:array[i][j]
array[i, j] 等效于 array[j],因为 i 被忽略。

【讨论】:

    【解决方案2】:

    c 中的数组不像Grid[i, j] 那样访问,而是像Grid[i][j] 那样访问。因此,将其更改为您的代码中的那个。同样Pattern[i, j] 应该写成Pattern[i][j]

    这将解决您的问题。

    乐于助人! :)

    【讨论】:

      【解决方案3】:

      尝试以下方法:

      #include <stdio.h>
      
      int main() {
      
      int tc,T;
      printf("Enter one number\n");
      scanf("%d", &T);
      
      for(tc=0; tc<T; tc++){
      int flag=0;
      int R,C;
      printf("Enter 2 numbers\n");
      scanf("%d %d", &R, &C);
      {
          int i,j;
          int r,c;
          int Grid[R][C];
      
          printf("now reading the grid\n");
          //Read the Grid
          for(i=0; i<R; i++){
              for(j=0; j<C; j++){
                  scanf("%d", &Grid[i][j]);
              }
          }
      
          printf("Enter 2 mre numbers\n");
          scanf("%d %d", &r, &c);
          {
              int Pattern[r][c];
              printf("now reading the pattern\n");
      
              //Read the Grid
              for(i=0; i<r; i++){
                  for(j=0; j<c; j++){
                      scanf("%d", &Pattern[i][j]);
                  }
              }
              //Here we have both the Grid and the Pattern
              for(i=0; i<R; i++){
                  for(j=0; j<C; j++){
                      if(Grid[i][j]==Pattern[0][0] && ((i+r)<=R) && ((j+c)<=C)){
                          int x,y;
                          int innerFlag=1;
      
                          for(x=0; x<r; x++){
                              for(y=0; y<c; y++){
                                  if(Grid[x+i][y+j]!=Pattern[x][y]) innerFlag=0;
                              }
                          }
      
                          if(innerFlag == 1){ 
                              //Set the flag to 1 and break out of the nested loops
                              flag=1;
                              j=C;
                              i=R;
                          }
                      }
                  }
              }
      
              //Here all the calculation is done and the flag is set
              if(flag==1) printf("YES\n");
              else printf("NO\n");
          }
      
      }
      }
      
      return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-26
        • 1970-01-01
        • 2020-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多