2016-04-10 94 views
0

我有以下代码:错误scanf函数

#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' 需要类型的参数 '诠释' ,但参数2的类型为int()[(sizetype)(C)]' scanf(“%d”,& Grid [i,j]);
  2. 警告:格式 '%d' 期望类型的参数 '诠释',但参数2具有输入 'INT()[(的SizeType)(C)]' 的scanf( “%d”, & Pattern [i,j]);

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

P.S我也检查了网格和模式,它读取垃圾!

+0

规范表格/模式?是否为两个类都重载'[]'作为参数? – mwm314

+0

@ mwm314问题标记为C,代码看起来是C,所以没有超载。 – user3386109

+0

'Grid [i,j]'中的逗号被解释为[逗号运算符](https://en.wikipedia.org/wiki/Comma_o​​perator),所以'Grid [i,j]'与'格[j]的'。 – user3386109

回答

3

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

1

c中的数组不能像Grid[i, j]那样访问,而是作为Grid[i][j]访问。所以把它改成你的代码。同样Pattern[i, j]应写为Pattern[i][j]

这将解决您的问题。

乐意帮忙! :)

0

尝试类似如下:

#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; 
}