1//找出一个二维数组的“鞍点”,即该位置上的元素在该行上最大,在该列上最小。也可能没有鞍点。
2
3//(1)二维数组有鞍点:
4 int a[3][4]={{9,80,205,40},{90,-60,96,1},{210,-3,101,89}};
5
6 //(2)二维数组没有鞍点:
7 int b[4][4]={{9,80,205,40},{90,-60,196,1},{210,-3,101,89},{45,54,156,7}};
8
9 AnDian(a, 3);
10 AnDian(b, 4);
11
12/*
13查找鞍点函数
14注意:二维数组作参数,一定要给出列的维度
15*/
16void AnDian(int arr[][4], int len)
17{
18 int i, j, k;
19 int rowMaxIndex, iRAn=-1, iCAn=-1, iAn=-1;
20 int iTemp;
21 bool bAn = true;
22
23 printf("\n---------------------------------\n\n");
24 for (i=0; i<len; i++)
25 {
26 for (j=0; j<4; j++)
27 {
28 printf("%-5d ",arr[i][j]);
29 }
30 printf("\n\n");
31 }
32 //------------------------------
33 //find max number in row
34 for (i=0; i<len; i++)//Row
35 {
36 bAn = true;
37 iTemp = arr[i][0];
38 rowMaxIndex = 0;
39 for (j=0; j<4; j++)//Col
40 {
41 if (iTemp < arr[i][j])
42 {
43 iTemp = arr[i][j];
44 rowMaxIndex = j;
45 }
46 }
47
48 //judge wether iTemp is min number in Cols
49 for (k=0; k<len; k++)//Row
50 {
51 if (iTemp > arr[k][rowMaxIndex])
52 {
53 bAn = false;
54 break;
55 }
56 }
57 if (bAn)
58 {
59 iRAn = i;
60 iCAn = rowMaxIndex;
61 iAn = iTemp;
62 }
63 }// for (i=0; i<len; i++)//Row
64 if (iRAn != -1)
65 {
66 printf("鞍点位置:(%d, %d) 鞍点值:%d\n\n", iRAn, iCAn, iAn);
67 }
68 else
69 {
70 printf("没有鞍点\n\n");
71 }
72}