萌新求救,全局变量和局部变量在洛谷评测系统这里有什么限制吗?
查看原帖
萌新求救,全局变量和局部变量在洛谷评测系统这里有什么限制吗?
1096647
Auro_楼主2023/11/7 10:47

这两段代码在vs2022运行的结果都相同。 但第一段用的是全局变量,在洛谷里有两个测试点RE了:

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>  
#include <string.h>  
#include <stdio.h>
int arr[110][110] = { 0 };
int main()
{
   int n, m, k;
   int x, y;
   int num = 0;
   //int arr[110][110] = { 0 };
   scanf("%d %d %d", &n, &m, &k);
   for (int i = 1; i <= m; i++)
   {
   	scanf("%d %d", &x, &y);
   	arr[x][y] = 1;//1
   	arr[x - 1][y + 1] = 1;//2
   	arr[x - 1][y - 1] = 1;//3
   	arr[x + 1][y + 1] = 1;//4
   	arr[x + 1][y - 1] = 1;//5
   	arr[x - 1][y] = 1;//6
   	arr[x - 2][y] = 1;//7
   	arr[x + 1][y] = 1;//8
   	arr[x + 2][y] = 1;//9
   	arr[x][y - 1] = 1;//10
   	arr[x][y - 2] = 1;//11
   	arr[x][y + 1] = 1;//12
   	arr[x][y + 2] = 1;//13
   }
   for (int j = 1; j <= k; j++)
   {
   	scanf("%d %d", &x, &y);
   	for (int h = x - 2; h <= x + 2; h++)
   	{
   		for (int l = y - 2; l <= y + 2; l++)
   		{
   			arr[h][l] = 1;
   		}
   	}
   }
   for (int i = 1; i <= n; i++)
   {
   	for (int j = 1; j <= n; j++)
   	{
   		if (!arr[i][j]) { num = num + 1; }
   	}
   }
   printf("%d", num);
   return 0;
}

第二段代码把arr改为局部变量就过了

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>  
#include <string.h>  
#include <stdio.h>
//int arr[110][110] = { 0 };
int main()
{
   int n, m, k;
   int x, y;
   int num = 0;
   int arr[110][110] = { 0 };
   ...//同上
}

这是为什么啊!!!

2023/11/7 10:47
加载中...