-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");
}
}
感謝を。 – holicmiku