在 C++ 中查詢方陣對角線上的最小和最大元素


在這個問題中,我們給定一個大小為 nXn 的方陣。我們的任務是查詢方陣對角線上的最小和最大元素。我們需要找到矩陣的主對角線和副對角線的最小和最大元素。

讓我們舉個例子來理解這個問題,

輸入

mat[][] = {
   {3, 4, 7},
   {5, 2, 1},
   {1, 8, 6}
}

輸出

Smallest element in Primary Diagonal = 2
Largest element in Primary Diagonal = 6
Smallest element in Secondary Diagonal = 1
Largest element in Secondary Diagonal = 7

解決方案方法

解決這個問題的一個簡單方法是使用巢狀迴圈。為了檢查主對角線上的元素,我們將考慮 **i = j**。對於副對角線,我們將考慮 **i + j = n -1**。我們將找到主對角線和副對角線矩陣的最大和最小元素。

程式說明我們解決方案的工作原理,

示例

 即時演示

#include<iostream>
using namespace std;
void findMaxAndMinOfDiagonals(int mat[3][3], int n){
   if (n == 0)
      return;
   int pDiagMin = mat[0][0],
   pDiagMax = mat[0][0];
   int sDiagMin = mat[0][n - 1 ],
   sDiagMax = mat[0][n - 1];
   for (int i = 1; i < n; i++) {
      for (int j = 1; j < n; j++) {
         if (i == j){
            if (mat[i][j] < pDiagMin)
               pDiagMin = mat[i][j];
            if (mat[i][j] > pDiagMax)
            pDiagMax = mat[i][j];
         }
         if ((i + j) == (n - 1)) {
            if (mat[i][j] < sDiagMin){
               sDiagMin = mat[i][j];
            }
            if (mat[i][j] > sDiagMax)
               sDiagMax = mat[i][j];
         }
      }
   }
   cout<<("\nSmallest Element of Principal Diagonal : ")<<pDiagMin;
   cout<<("\nGreatest Element of Principal Diagonal : ")<<pDiagMax;
   cout<<("\nSmallest Element of Secondary Diagonal : ")<<sDiagMin;
   cout<<("\nGreatest Element of Secondary Diagonal : ")<<sDiagMax;
}
int main(){
   int mat[3][3] = {
      { 3, 4, 7 },
      { 0, 2, 1 },
      { 1, 7, 8 }
   };
   int n = sizeof(mat) / sizeof(mat[0]);
   findMaxAndMinOfDiagonals(mat, n);
}

輸出

Smallest Element of Principal Diagonal : 2
Greatest Element of Principal Diagonal : 8
Smallest Element of Secondary Diagonal : 2
Greatest Element of Secondary Diagonal : 7

另一種更有效的解決方案是將巢狀迴圈減少為單個迴圈,利用主對角線矩陣的兩個索引相同,即

primary diagonal elements = mat[i][j].
Similarly, for secondary diagonal elements = mat[i][n - i - 1]

程式說明我們解決方案的工作原理,

示例

 即時演示

#include<iostream>
using namespace std;
void findMaxAndMinOfDiagonals(int mat[3][3], int n){
   if (n == 0)
      return;
   int pDiagMin = mat[0][0],
   pDiagMax = mat[0][0];
   int sDiagMin = mat[0][n - 1 ],
   sDiagMax = mat[0][n - 1];
   for (int i = 1; i < n; i++) {
      if (mat[i][i] < pDiagMin)
         pDiagMin = mat[i][i];
      if (mat[i][i] > pDiagMax)
         pDiagMax = mat[i][i];
      if (mat[i][n - 1 - i] < sDiagMin)
         sDiagMin = mat[i][n - 1 - i];
      if (mat[i][n - 1 - i] > sDiagMax)
         sDiagMax = mat[i][n - 1 - i];
   }
   cout<<("\nSmallest Element of Principal Diagonal : ")<<pDiagMin;
   cout<<("\nGreatest Element of Principal Diagonal : ")<<pDiagMax;
   cout<<("\nSmallest Element of Secondary Diagonal : ")<<sDiagMin;
   cout<<("\nGreatest Element of Secondary Diagonal : ")<<sDiagMax;
}
int main(){
   int mat[3][3] = {
      { 3, 4, 7 },
      { 0, 2, 1 },
      { 1, 7, 8 }
   };
   int n = sizeof(mat) / sizeof(mat[0]);
   findMaxAndMinOfDiagonals(mat, n);
}

輸出

Smallest Element of Principal Diagonal : 2
Greatest Element of Principal Diagonal : 8
Smallest Element of Secondary Diagonal : 1
Greatest Element of Secondary Diagonal : 7

更新於: 2021-03-16

1K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.