2016-08-08 5 views
-4

これら2つのコードの違いは何ですか?私はそれが同じコードだと思う。Cプログラミング/これらの2つのコードの違い

codeupとVisual Studioの両方を操作することはできません。あなたはそれが例えばNUM * num個の配列

を作る 番号を入れると、アレイ ため codeup.kr 1505問題

問題は、NUM = 3与えられ、それが作成されます。

1 2 3 

8 9 4 

7 6 5 

コードを1:

#include<stdio.h> 
#include<string.h> 

int main() 
{ 
int tile[52][52]; 
int x, y; 
int num; 
int start = 1; 
int check = 1, complete = 1; 
int i; 

scanf("%d", &num); 
memset(tile, 0, 4 * 52 * 52); 


tile[0][1] = 10; 
tile[1][num + 1] = 10; 
tile[num + 1][num + 1] = 10; 
tile[num + 1][0] = 10; 

x = 1; y = 1; 
for (i = 0; i <= 3000; i++) 
{ 
    switch (check) 
    { 
    case 1: 
    { 
     if (tile[x][y + 1] != 0) 
     { 

      check += 1; 
      break; 
     } 
     tile[x][y++] = start; 
     start++; 
     complete++; 
     break; 

    } 
    case 2: 
    { 
     if (tile[x + 1][y] != 0) 
     { 
      check += 1; 
      break; 
     } 
     tile[x++][y] = start; 
     start++; 

     complete++; 
     break; 
    } 
    case 3: 
    { 
     if (tile[x][y - 1] != 0) 
     { 
      check += 1; 
      break; 
     } 
     tile[x][y--] = start; 
     start++; 
     complete++; 
     break; 
    } 
    case 4: 
    { 
     if (tile[x - 1][y] != 0) 
     { 
      check = 1; 
      break; 
     } 
     tile[x--][y] = start; 
     start++; 
     complete++; 
     break; 
    } 

    } 
    if (complete == num*num) 
    { 
     break; 
    } 
} 


if (num % 2 == 0) 
{ 
    tile[x][y] = start; 
} 
else 
{ 
    tile[x][y++] = start; 
} 


for (x = 1;x <= num; x++) 
{ 
    for (y = 1; y <= num; y++) 
    { 
     printf("%d ", tile[x][y]); 
    } 
    printf("\n"); 
} 

} 

このコードはVisual Studioで動作します。

#include<stdio.h> 


int main() 
{ 
int tile[52][52]; 
int x, y; 
int num; 
int start = 1; 
int check = 1, complete = 1; 
int i; 

scanf("%d", &num); 
for (x = 0;x <= num; x++) 
{ 
    for (y = 0; y <= num; y++) 
    { 
     tile[x][y] = 0; 
    } 
} 
tile[0][1] = 10; 
tile[1][num+1] = 10; 
tile[num + 1][num + 1] = 10; 
tile[num + 1][0] = 10; 

x = 1; y = 1; 
for (i = 0; i <= 3000; i++) 
{ 
    switch (check) 
    { 
    case 1: 
    { 
     if (tile[x][y + 1] != 0) 
     { 

      check += 1; 
      break; 
     } 
     tile[x][y++] = start; 
     start++; 
     complete++; 
     break; 

    } 
    case 2: 
    { 
     if (tile[x + 1][y] != 0) 
     { 
      check += 1; 
      break; 
     } 
     tile[x++][y] = start; 
     start++; 

     complete++; 
     break; 
    } 
    case 3: 
    { 
     if (tile[x][y - 1] != 0) 
     { 
      check += 1; 
      break; 
     } 
     tile[x][y--] = start; 
     start++; 
     complete++; 
     break; 
    } 
    case 4: 
    { 
     if (tile[x - 1][y] != 0) 
     { 
      check = 1; 
      break; 
     } 
     tile[x--][y] = start; 
     start++; 
     complete++; 
     break; 
    } 

    } 
    if (complete == num*num) 
    { 
     break; 
    } 
} 

if (num % 2 == 0) 
{ 
    tile[x][y] = start; 
} 
else 
{ 
    tile[x][y++] = start; 
} 


for (x = 1;x <= num; x++) 
{ 
    for (y = 1; y <= num; y++) 
    { 
     printf("%d ", tile[x][y]); 
    } 
    printf("\n"); 
} 

} 

答えて

1

差:

$ diff -w one.c two.c2,3d1 

< #include<string.h> 
< 
14,15c12,16 
< memset(tile, 0, 4 * 52 * 52); 
< 
--- 
> for (x = 0; x <= num; x++) { 
>  for (y = 0; y <= num; y++) { 
>  tile[x][y] = 0; 
>  } 
> } 
95a97 
> 

ので、最初のものは、memsetを0(ゼロ)と第二のものにtile全てエントリを設定するために使用するには、のみ設定するループを使用しますtileのエントリはゼロに使用され、セルの残りの内容は定義されておらず、すべてである可能性があります。 xおよび/またはy0 <= x <= num < 52の外にある場所をデバッグするか、またはprintf()を使って正確な場所を見つけることができます。

あなたはmemset()すぎたりはできません使用しない場合:ちょうどゼロにすべてのセルをループを変更する:あなたのアドバイスのための

for (x = 0; x < 52; x++) { 
    for (y = 0; y < 52; y++) { 
    tile[x][y] = 0; 
    } 
} 
+0

感謝を。 – holicmiku

関連する問題