C++ &引用;使用二维数组查找矩阵中的最大乘积;
你好!今天,我有一个关于如何开始我的作业的问题。作业的目标是创建一个函数,find_best_product(),它接受一个数组 参数,并返回四个相邻数字的最大乘积,这些数字可以是上、下、, 在N×M网格/矩阵中以左、右、对角或长方体形式排列 为了便于测试,我已经发布了创建数组并用0到50的随机数填充数组的精确代码。我不知道的是如何创建一个算法,可以扫描整个阵列的目标。我有一种方法可以在一个2x2的盒子里找到最好的产品,但是直线和对角线很难找到。欢迎提供任何帮助、意见或建议。以下是我的现有代码:C++ &引用;使用二维数组查找矩阵中的最大乘积;,c++,arrays,algorithm,C++,Arrays,Algorithm,你好!今天,我有一个关于如何开始我的作业的问题。作业的目标是创建一个函数,find_best_product(),它接受一个数组 参数,并返回四个相邻数字的最大乘积,这些数字可以是上、下、, 在N×M网格/矩阵中以左、右、对角或长方体形式排列 为了便于测试,我已经发布了创建数组并用0到50的随机数填充数组的精确代码。我不知道的是如何创建一个算法,可以扫描整个阵列的目标。我有一种方法可以在一个2x2的盒子里找到最好的产品,但是直线和对角线很难找到。欢迎提供任何帮助、意见或建议。以下是我的现有代码:
int find_greatest_product(int **newArray, int &r, int &c){
//find the greatest product of 4 adjacent numbers, in any direction.
}
int test_array(){
//error handling
}
int print_array(int **newArray, int &r, int &c){
for (int i = 0; i < c; ++i)
{
for (int j = 0; j < r; ++j)
{
cout << newArray[i][j] << ' ';
}
cout << endl;
}
}
int create_array(int r, int c){
srand((unsigned)time(0));
int** newArray = new int*[r];
for (int i = 0; i < r; ++i)
{
newArray[i] = new int[c];
for (int j = 0; j < c; ++j)
newArray[i][j] = rand() % 50 + 1;
}
print_array(newArray, r, c);
}
int main(int argc, char* argv[]) {
int rows, col;
cout << "enter rows : " << endl;
cin >> rows;
cout << "enter cols : " << endl;
cin >> col;
create_array(rows, col);
}
int查找最伟大的产品(int**newArray、int&r、int&c){
//求任意方向上4个相邻数字的最大乘积。
}
int测试_数组(){
//错误处理
}
int打印数组(int**newArray、int&r、int&c){
对于(int i=0;i cout此算法将为您提供最大乘积。但请试运行此代码并了解此算法的工作原理。我尚未添加查找行、列或方向(垂直、向下等)的功能。请思考并更新此算法以添加这些功能
int find_greatest_product(int **newArray, int &r, int &c)
{
int max_product=0, p1=0, p2=0, p3=0;
for(int i=0; i< r-3; i++)
{
for(int j=0; j<c-3; j++)
{
//down
max_product = newArray[i][j]*newArray[i+1][j]*newArray[i+2][j]*newArray[i+3][j];
//right_horizental
p1 = newArray[i][j]*newArray[i][j+1]*newArray[i][j+2]*newArray[i][j+3];
if(p1 > max_product)
max_product = p1;
//diagonally_right_down
p2 = newArray[i][j]*newArray[i+1][j+1]*newArray[i+2][j+2]*newArray[i+3][j+3];
if(p2 > max_product)
max_product = p2;
//box
p3 = newArray[i][j]*newArray[i][j+1]*newArray[i+1][j]*newArray[i+1][j+1];
if(p3 > max_product)
max_product = p3;
}
}
return max_product;
}
int查找最伟大的产品(int**newArray、int&r、int&c)
{
int max_乘积=0,p1=0,p2=0,p3=0;
对于(int i=0;i最大乘积)
最大乘积=p2;
//盒子
p3=newArray[i][j]*newArray[i][j+1]*newArray[i+1][j]*newArray[i+1][j+1];
如果(p3>最大乘积)
最大乘积=p3;
}
}
返回最大值产品;
}
这里面有一点错误。我已经改正了。希望它现在可以正常工作。你可以比较两个代码,看看我的错误是什么
int find_greatest_product(int **newArray, int &r, int &c)
{
int max_product=0, p0 = 0, p1=0, p2=0, p3=0;
for(int i=0; i< r; i++)
{
for(int j=0; j<c; j++)
{
if(i < r-3)
{
//down
p0 = newArray[i][j]*newArray[i+1][j]*newArray[i+2][j]*newArray[i+3][j];
if(p0 > max_product)
max_product = p0;
}
if(i < r-3 && j < c-3 )
{
//diagonally_right_down
p2 = newArray[i][j]*newArray[i+1][j+1]*newArray[i+2][j+2]*newArray[i+3][j+3];
if(p2 > max_product)
max_product = p2;
}
if(j < c-3)
{
//right_horizental
p1 = newArray[i][j]*newArray[i][j+1]*newArray[i][j+2]*newArray[i][j+3];
if(p1 > max_product)
max_product = p1;
}
if(i < r-1 && j < c-1)
{
//box
p3 = newArray[i][j]*newArray[i][j+1]*newArray[i+1][j]*newArray[i+1][j+1];
if(p3 > max_product)
max_product = p3;
}
}
}
return max_product;
}
int查找最伟大的产品(int**newArray、int&r、int&c)
{
int max_乘积=0,p0=0,p1=0,p2=0,p3=0;
对于(int i=0;i最大乘积)
最大乘积=p2;
}
if(j最大乘积)
最大乘积=p1;
}
如果(i最大乘积)
最大乘积=p3;
}
}
}
返回最大值产品;
}
int find\u最大的产品(int**newArray、int&r、int&c)
{
//求任意方向上4个相邻数字的最大乘积。
//更大的计算定义了我们结果的属性(坐标、sahp“方向”和当前乘法“produit”)
greaterCalc最大值(0,0,0,“无”);
对于(int i=0;i=4)
{
对于(int l=j,k=0;k<4;k++,l++)
produitHorizontal*=newArray[i][l];
//如果产品超过了我们的MaxP,我们只需更新,然后继续
if(produitHorizontal>maxP.produit)
{
maxP.x=j;
maxP.y=i;
maxP.produit=produitHorizontal;
maxP.direction=“水平”;
}
}
//我们在这里检查4个垂直的邻居
如果(r-i>=4)
{for(int l=i,k=0;k<4;k++,l++)
produitVertical*=newArray[l][j];
//如果产品超过了我们的MaxP,我们只需更新,然后继续
if(produitVertical>maxP.produit)
{
maxP.x=j;
maxP.y=i;
maxP.produit=produitVertical;
maxP.direction=“垂直”;
}
}
//我们在这里检查4个对角邻居
如果(r-i>=4&&c-j>=4)
{
对于(int l=i,m=j,k=0;k<4;k++,l++,m++)
produitDiagonal*=新数组[l][m];
//如果产品超过了我们的MaxP,我们只需更新,然后继续
if(produitDiagonal>maxP.produit)
{
maxP.x=j;
maxP.y=i;
maxP.produit=produitDiagonal;
maxP.direction=“对角线”;
}
}
}
}
一个有趣的问题。请解释如何在代码中传递在create\u array()中创建的数组
back to main?@George Gall有许多用户使用nick Stackoverflow。你在问候什么用户?:)@Christophe可以在main中取消引用它吗?它们的哪一部分被证明是困难的?你是如何解决问题的?好的!让我们一起做一些进展:开始更改代码,以便返回数组指针to main()。要验证它是否有效,请从main调用print_数组()。最后以某种方式组织该数组
int find_greatest_product (int **newArray, int &r, int &c)
{
//find the greatest product of 4 adjacent numbers, in any direction.
//greater Calc deisgnes the attributes of our result ( the coordonates , the sahp "direction" and the current multiplication "produit")
greaterCalc maxP(0,0,0,"none");
for(int i = 0 ; i < r ; i++)
{
for(int j = 0; j < c ; j++)
{
long produitHorizontal = 1,produitVertical = 1, produitDiagonal=1;
//We check here the 4 horizontal neighbours
if(c-j >= 4)
{
for(int l = j,k=0 ; k < 4 ;k++, l++)
produitHorizontal *= newArray[i][l];
//If the product is more than our MaxP, we just update, and we continue
if(produitHorizontal > maxP.produit )
{
maxP.x=j;
maxP.y=i;
maxP.produit = produitHorizontal;
maxP.direction = "horizontal";
}
}
//We check here the 4 vertical neighbours
if(r-i >= 4)
{ for(int l = i, k=0 ; k < 4 ;k++,l++)
produitVertical *= newArray[l][j];
//If the product is more than our MaxP, we just update, and we continue
if(produitVertical > maxP.produit )
{
maxP.x=j;
maxP.y=i;
maxP.produit = produitVertical;
maxP.direction = "vertical";
}
}
//We check here the 4 diagonal neighbours
if(r-i >= 4 && c-j >= 4 )
{
for(int l = i,m = j, k=0 ; k < 4 ;k++,l++,m++)
produitDiagonal *= newArray[l][m];
//If the product is more than our MaxP, we just update, and we continue
if(produitDiagonal > maxP.produit )
{
maxP.x=j;
maxP.y=i;
maxP.produit = produitDiagonal;
maxP.direction = "diagonal";
}
}
}
}
cout<<endl<<endl <<"Max Prod : "<<maxP.produit <<" Position : row"<<maxP.y +1 <<", col" <<maxP.x +1 <<" Shape : "<<maxP.direction <<endl;
return 0;
}