系统给定外部整型变量nsmax和整型二维数组a (不雾要自行定义)。编写程序,求n*n二维数组a的周边元素的最大值smaX。
时间: 2023-02-06 19:06:49 浏览: 66
在这个程序中,你需要遍历二维数组a的每一个元素,并找出其周边元素中的最大值。为了方便遍历,你可以使用两个循环嵌套,第一个循环遍历数组a的行,第二个循环遍历数组a的列。
在每一次遍历时,你可以使用一个临时变量temp来保存当前遍历到的元素周边元素的最大值。你可以在循环体内部使用if语句来比较当前元素周边元素的值,并使用max函数来找出最大值。
在每一次遍历完成后,你还需要使用if语句来比较当前的最大值temp和最终的最大值smax,并使用max函数来更新最终的最大值。
下面是使用C++语言编写的程序示例:
```
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n, a[N][N], smax;
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int temp = a[i][j];
if (i > 0) temp = max(temp, a[i-1][j]);
if (i < n-1) temp = max(temp, a[i+1][j]);
if (j > 0) temp = max(temp, a[i][j-1]);
if (j < n-1) temp = max(temp, a[i][j+1]);
smax = max(smax, temp);
}
}
cout << smax <<