问题描述
此代码只能返回矩阵中的一个最小元素,但是如果我想返回每一行中的最小元素,该怎么办?我需要在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,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,index);
return 0;
}
解决方法
我认为,如果您使用标准算法-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
时修复row的索引。下面是一些不言自明的代码:
#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';
}
,
您可以编写一个递归函数,以查找一维数组中最小的元素,该数组将针对数组数组或二维数组的每个“行”进行调用。
这是一个演示程序。
#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的情况下调用该函数。在这种情况下,返回任何整数值都没有意义,因为它可以与实际值。