C编程/这两个代码之间有什么区别

时间:2016-08-08 02:22:13

标签: c

这两个代码有什么区别?我认为它的代码相同。

它无法同时运行代码和Visual Studio。 codeup.kr 1505问题

数组的问题 当你把它编号 它使num * num数组

例如,给定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 个答案:

答案 0 :(得分: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将tile所有条目设置为0(零),第二个使用循环设置使用的tile到零的条目,其余单元格的内容未定义,可能是一切。您可以在代码中查找x和/或y超出0 <= x <= num < 52的某些位置,或者使用调试器,或使用printf()来查找确切的位置

如果你不想使用memset()或者也不能使用{p>,只需将每个单元格的循环更改为零:

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