就解题而言的话, 这题不用动态分配内存和指针, 如下代码就行:
int main()
{
int x, y;
scanf("%d %d", &y, &x);
int num = 0;
int sum = 0;
for (int y0=0; y0
for (int x0=0; x0
scanf("%d", &num);
if (y0 == 0 || y0 == y - 1 || x0 == 0 || x0 == x - 1)
sum += num;
}
}
printf("%d", sum);
return 0;
}
但你一定要动态分配和指针版本的话, 那就下面这样:
int main()
{
int x, y;
scanf("%d %d", &y, &x);
int x0, y0;
int* pNum = new int[y * x];
for (y0=0; y0
for (x0=0; x0
scanf("%d", &pNum[y0 * x + x0]);
}
}
int sum = 0;
for (y0=0; y0
for (x0=0; x0
if (y0 == 0 || y0 == y - 1 || x0 == 0 || x0 == x - 1)
sum += pNum[y0 * x + x0];
}
}
printf("%d", sum);
return 0;
}