2016-04-10 268 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; 
}