寻找鞍点
给定一个5*5的矩阵,每行只有一个最大值,每列只有一个最小值,寻找这个矩阵的鞍点。
鞍点指的是矩阵中的一个元素,它是所在行的最大值,并且是所在列的最小值。
1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 5 char checkrow(int rownum); 6 char checkcol(int value,int colnum); 7 int a[5][5]; 8 9 int main(int argc, char** argv) { 10 //freopen("c:\\test.txt","r",stdin); 11 12 int i,j,col; 13 for(i=0;i<5;i++) 14 { 15 for(j=0;j<5;j++) 16 { 17 cin>>a[i][j]; 18 } 19 } 20 21 for(i=0;i<5;i++) 22 { 23 col=checkrow(i); //本行最大 24 if(checkcol(a[i][col],col)) //是本列最小 25 { 26 printf("%d %d %d\n",i+1,col+1,a[i][col]); 27 return 0; 28 } 29 } 30 31 printf("not found"); 32 33 return 0; 34 } 35 36 char checkrow(int rownum) 37 { 38 int temp,x,cols; 39 temp=a[rownum][0]; 40 cols=0; 41 for(x=1;x<5;x++) 42 { 43 if(a[rownum][x]>temp) 44 { 45 temp=a[rownum][x]; 46 cols=x; 47 } 48 } 49 50 return cols; 51 } 52 53 char checkcol(int value,int colnum) 54 { 55 int y,temp; 56 temp=value; 57 for(y=0;y<5;y++) 58 { 59 if(a[y][colnum]<temp) 60 return 0; 61 } 62 63 return 1; 64 }
posted on 2016-04-26 20:06 foggia2004 阅读(461) 评论(0) 收藏 举报
浙公网安备 33010602011771号