找鞍点作业(07次)

#include 
#include 
#include 
#include 

#define M 4
#define N 4

typedef struct {
  int value[M][N];
  int count[M][N];//作为判断矩阵,其被初始化为0
} Array;

int main(void) {
  Array a;
  int i,j,k;
  memset(a.count, 0, M * N * sizeof(int));
  srand((unsigned)time(NULL));

  for (i = 0; i < M; i++) {//随机给value矩阵赋值
    for (j = 0; j < N; j++) {
      a.value[i][j] = rand() % 21;
      printf("%d\t", a.value[i][j]);
    }
    printf("\n");
  }

  printf("\n");

  for (i = 0; i < M; i++) {//遍历横排,标记每排最小值
    k = 0;
    for (j = 0; j < N; j++) {
      if (a.value[i][j] < a.value[i][k]) {
        k = j;
      }
    }
    a.count[i][k]++;
  }

  for (j = 0; j < N; j++) {//遍历竖列,标记每列最大值
    k = 0;
    for (i = 0; i < M; i++) {
      if (a.value[i][j] > a.value[k][j]) {
        k = i;
      }
    }
    a.count[k][j]++;
  }

  for (i = 0; i < M; i++) {
    for (j = 0; j < N; j++) {
      if (a.count[i][j] == 2) {
        printf("row %d rank %d: %d\n", i+1, j+1, a.value[i][j]);
        break;
      }
    }
  }
  getchar(); 
  return 0;
}

你可能感兴趣的:(找鞍点作业(07次))