C语言经典算法【每日一练】18

题目:找出矩阵的最大值和最小值所在的行和列

#include

int main() {
//找出矩阵的最大值和最小值及所在的行和列
  int a[5][5]={{5,4,3,2,1},
               {10,9,8,7,6},
         {5,8,2,3,4},
         {3,3,2,5,8},
         {4,3,8,9,10}};
  int max=a[0][0],min=a[0][0],i,j, max_i,max_j,min_i,min_j;
  for(i=0;i<=4;i++)
    {
      for(j=0;j<=4;j++)
       {
        if (a[i][j] > max) {
          max = a[i][j];
          max_i = i;
          max_j = j;
        }
        if (a[i][j] < min) {
          min = a[i][j];
          min_i = i;
          min_j = j;
        }
       }
     }
    printf("max is %d and the min is %d\n", max, min);
    printf("max row and column is %d %d\n", max_i, max_j);
    printf("min row and column is %d %d\n", min_i, min_j);
   return 0;
}

执行结果:

C语言经典算法【每日一练】18_第1张图片

你可能感兴趣的:(C语言,算法,c语言,c++)