using System;
class Program3
{
public static void Main()
int[,] arr = { { 1, 3, 2 }, { 9, 8, 7 }, { 4, 5, 6 }, { 0, 4, 8 } };
int rows = 4;
int cols = 3;
int count = GetMaxMinValue(arr, rows, cols);
if (count < 1)
Console.WriteLine("當前二維陣列arr沒有鞍點:");
}
//二維陣列中的鞍點,即該位置上的元素在該行上最大,在該列上最小(也可能沒有鞍點)
public static int GetMaxMinValue(int[,] arr, int rows, int cols)
int i, j, k;
int res;
int count = 0;
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++)
res = arr[i, j];
//判斷res在當前行是否是最大的
for (k = 0; k < cols; k++)
if (arr[i, k] > res)
break;
if (k < cols)
continue;
//判斷res在當行列是否是最小的
for (k = 0; k < rows; k++)
if (arr[k, j] < res)
if (k < rows)
count++;
Console.WriteLine("鞍點A[{0},{1}] = {2}", i, j, arr[i, j]);
return count;
using System;
class Program3
{
public static void Main()
{
int[,] arr = { { 1, 3, 2 }, { 9, 8, 7 }, { 4, 5, 6 }, { 0, 4, 8 } };
int rows = 4;
int cols = 3;
int count = GetMaxMinValue(arr, rows, cols);
if (count < 1)
{
Console.WriteLine("當前二維陣列arr沒有鞍點:");
}
}
//二維陣列中的鞍點,即該位置上的元素在該行上最大,在該列上最小(也可能沒有鞍點)
public static int GetMaxMinValue(int[,] arr, int rows, int cols)
{
int i, j, k;
int res;
int count = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
res = arr[i, j];
//判斷res在當前行是否是最大的
for (k = 0; k < cols; k++)
{
if (arr[i, k] > res)
break;
}
if (k < cols)
{
continue;
}
//判斷res在當行列是否是最小的
for (k = 0; k < rows; k++)
{
if (arr[k, j] < res)
break;
}
if (k < rows)
{
continue;
}
count++;
Console.WriteLine("鞍點A[{0},{1}] = {2}", i, j, arr[i, j]);
}
}
return count;
}
}