【问题标题】:Smallest element in each row of matrix C++矩阵C ++每行中的最小元素
【发布时间】:2021-01-13 04:29:12
【问题描述】:

这段代码只能返回一个矩阵中的最小元素,但是如果我想返回每一行中的最小元素呢?我需要在 C++ 中使用递归函数。感谢您的帮助

#include<iostream>
using namespace std;
int smallest(int** arr, int rows, int columns, int column_index = 0)
{
    if (rows <= 0 || column_index >= columns)
        return INT_MAX;


    if (rows == 1)
        return min(*(*arr + column_index),       
            smallest(arr, 1, columns - 1,
                column_index + 1)); 


    return min(smallest(arr, 1, columns), 
        smallest(arr + 1, rows - 1, columns));
}
int main()
{
    int row, col, index=0;
    cin >> row;
    cin >> col;
    int** arr;
    arr = new int* [row];
    for (int i = 0; i < row; i++) {
         arr[i] = new int[col];
         for (int j = 0; j < col; j++) {
             cin >> arr[i][j];
           }
        }
    cout<<smallest(arr, row, col, index);
    return 0;
}

【问题讨论】:

    标签: c++ algorithm recursion min function-definition


    【解决方案1】:

    如果您使用标准算法,我认为这么多代码就足够了 - std::min_element:

    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    int main() {
        int r, c;
        std::cin >> r >> c;
        std::vector<std::vector<int>> mat(r, std::vector<int>(c));
        for (auto &&row : mat)
            for (auto &&ele : row)
                std::cin >> ele;
        for (auto &&row : mat)
            std::cout << *std::min_element(row.begin(), row.end()) << std::endl;
    }
    

    如果您想按照自己的方式进行操作(老派风格,使用递归),请执行以下操作。您只需要在调用smallest 时修复行的索引。下面是一些不言自明的代码:

    #include <algorithm>
    #include <iostream>
    
    // here row_index represents the index of row and col represents the number of
    // elements in that row which are not yet traversed (counted from beginning)
    int smallest(int **arr, int row_index, int col) {
    
        // only first element is not traversed
        if (col == 1)
            return arr[row_index][0];
    
        // return minimum of last element and value returned by recursive call for
        // first col - 1 elements
        return std::min(arr[row_index][col - 1], smallest(arr, row_index, col - 1));
    }
    
    int main() {
        int row, col;
        std::cin >> row;
        std::cin >> col;
        int **arr = new int *[row];
        for (int i = 0; i < row; i++) {
            arr[i] = new int[col];
            for (int j = 0; j < col; j++)
                std::cin >> arr[i][j];
        }
    
        // call the function for each row
        for (int i = 0; i < row; i++)
            std::cout << "Smallest element in row " << i + 1 << " : "
                      << smallest(arr, i, col) << '\n';
    }
    

    【讨论】:

      【解决方案2】:

      您可以编写一个递归函数,找出一维数组中的最小元素,该函数将为数组数组或二维数组的每一“行”调用。

      这是一个演示程序。

      #include <iostream>
      #include <iomanip>
      #include <algorithm>
      #include <cstdlib>
      #include <ctime>
      
      const int * smallest( const int *a, size_t n )
      {
          return n < 2 ? a  
                       : std::min( a, smallest( a + 1, n - 1 ), 
                                   []( const int *p1, const int *p2 )
                                   {
                                      return not ( *p2 < *p1 );
                                   } );
      }
      
      int main() 
      {
          std::srand( ( unsigned int )std::time( nullptr ) );
          size_t rows, cols;
          
          std::cin >> rows >> cols;
          
          int **a = new int *[rows];
          
          for ( size_t i = 0; i < rows; i++ )
          {
              a[i] = new int[cols];
              for ( size_t j = 0; j < cols; j++ )
              {
                  a[i][j] = std::rand() % ( rows + cols );
              }
          }
          
          for ( size_t i = 0; i < rows; i++ )
          {
              for ( size_t j = 0; j < cols; j++ )
              {
                  std::cout << std::setw( 2 ) << a[i][j] << ' ';
              }
              std::cout << '\n';
          }
          std::cout << '\n';
          
          for ( size_t i = 0; i < rows; i++ )
          {
              std::cout << std::setw( 2 ) << *smallest( a[i], cols ) << ' ';
          }
          std::cout << '\n';
          
          for ( size_t i = 0; i < rows; i++ )
          {
              delete [] a[i];
          }
          
          delete [] a;
      }   
      

      如果输入的行数和列数等于 10 则程序输出可能如下所示

       1 16  7  6  2  7  1 14  3  8 
       0 14  9  0  6 18 18  7  7 19 
      12 17  9 12 14 10  7  9 15  3 
      14  8 19 13 14  1 12 15  7 15 
      16  7  1 17 19  8 15 18  7 15 
       9 19 12 10  3 18  0 10  7  8 
       6 13 16 17  7  3 19 19 18  6 
       7 14  6  8  3 17  8 19  7 16 
       6 16  7 10 19 11  1 19 13  8 
      19 19 14  8 17  1 11  8 12  1 
      
       1  0  3  1  1  0  3  3  1  1 
      

      注意该函数应该返回一个指向最小元素的指针,因为通常可以使用等于 0 的第二个参数调用该函数。在这种情况下,返回任何整数值都没有意义,因为它可能与一个实际值。

      【讨论】:

      • 或返回std::optional&lt;int&gt;
      猜你喜欢
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 2016-09-18
      • 2011-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多