【问题标题】:"Finding the Greatest Products in a Matrix, Using a Two-Dimensional Arrays"“使用二维数组在矩阵中找到最大的产品”
【发布时间】:2015-03-04 20:46:26
【问题描述】:

你好 Stackoverflow!今天我有一个关于我的作业开始的问题。作业的目标是创建一个函数 find_greatest_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);

}

另外,对于那些感兴趣的人,这里是原始的问题陈述:

【问题讨论】:

  • 有趣的问题。解释一下如何在您的代码中将您在 create_array() 中创建的数组传递回 main ?
  • @George Gall 有很多用户使用 nick Stackoverflow。你在问候什么用户?:)
  • @Christophe 是否可以在主体中取消引用它?
  • 其中哪一部分被证明是困难的?您是如何尝试解决问题的?
  • 好的!让我们一起做一些进展:开始更改代码,以便将数组指针返回给 main()。要验证它是否有效,请从 main 调用 print_array()。最后以某种方式组织所有这些指针将在您离开 main() 时被释放。一旦这些基本的东西完成了,我会回到你身边

标签: c++ arrays algorithm


【解决方案1】:

这个算法会给你最大的产品。但是试运行这个代码并理解这个算法是如何工作的。我没有添加查找行、列或方向(垂直、向下等)的功能。思考并更新此算法以添加这些功能。

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 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;
}

【讨论】:

  • 这是一种与我自己提出的算法非常相似的算法。好答案!我将使用它制作一个测试程序,看看它是否有效。
  • 效果很好。我的另一个问题是,我可以在打印函数中添加什么,在单个数字前面添加一个“0”?
  • 这个问题与称为填充零的术语有关......以下帖子将帮助您做到这一点:stackoverflow.com/questions/1714515/…
【解决方案2】:
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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 2017-02-17
    • 2020-08-17
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多