寻找系列中最大的产品

时间:2017-04-02 01:06:19

标签: c++ multidimensional-array

我正在尝试解决以下问题:https://projecteuler.net/problem=8

基本上我正在浏览2D阵列并从八个方向(N,NW,NE,E,W,S,SW,SE)中的每一个获得13位数产品并将返回值设置为最大值(如果有的话。)

但我得到64497254400的答案,这是不正确的。关于我可能做错的任何提示?

以下是我直截了当,效率低下的解决方案:

#include <stdio.h>
#include <iostream>
#include <fstream>

using namespace std;

long long int MAX(long long int a, long long int b)
{
    if (a > b)
    {
        return a;
    }
    else
    {
        return b;
    }
}

void largest_product_in_series(int param)
{
    char filename[] = "8_.txt";
    char my_character ;
    int column = 0; int row = 0;
    int c = 0; int r = 0; int cc = 0; int rr = 0;
    int list[100][100];
    int num = param - 1;

    ifstream fin;
    fin.open(filename, ios::in);

    while (!fin.eof() ) 
    {
        fin.get(my_character);
        if (my_character == '\n')
        {
            row++;
            r++;
            c = 0;
            cout << endl;
        }
        else
        {
            if (row == 0)
            {
                column++;
            }
            list[r][c] = (my_character - '0');
            cout << list[r][c];
            c++;
        }
    }

    cout << "Column: " << column << endl;
    cout << "Row: " << row << endl;

    long long int greatest_product = 0;

    long long int product = 0;

    for (rr = 0; rr < row; rr++)
    {
        for (cc = 0; cc < column; cc++)
        {
            //cout << "Column: " << cc << " Row: " << rr << endl;
            if (rr >= num)
            {
                r = rr;
                c = cc;
                product = list[r--][c];

                for (int i = 0; i < num; i++)
                {
                    product *= list[r--][c];
                }
                greatest_product = MAX(greatest_product, product);
            }
            if ((rr + num) <= row)
            {
                r = rr;
                c = cc;
                product = list[r++][c];

                for (int i = 0; i < num; i++)
                {
                    product *= list[r++][c];
                }
                greatest_product = MAX(greatest_product, product);
            }
            if (cc >= num)
            {
                r = rr;
                c = cc;
                product = list[r][c--];

                for (int i = 0; i < num; i++)
                {
                    product *= list[r][c--];
                }
                greatest_product = MAX(greatest_product, product);
            }
            if ((cc + num) <= column)
            {
                r = rr;
                c = cc;
                product = list[r][c++];

                for (int i = 0; i < num; i++)
                {
                    product *= list[r][c++];
                }
                greatest_product = MAX(greatest_product, product);
            }
            if ((rr >= num) && (cc >= num)) // NW
            {
                r = rr;
                c = cc;
                product = list[r--][c--];
                for (int i = 0; i < num; i++)
                {
                    product = list[r--][c--];
                }
                greatest_product = MAX(greatest_product, product);
            }
            if ((rr >= num) && ((cc + num) <= column)) // NE
            {
                r = rr;
                c = cc;
                product = list[r--][c++];
                for (int i = 0; i < num; i++)
                {
                    product = list[r--][c++];
                }
                greatest_product = MAX(greatest_product, product);
            }
            if (((rr + num) <= row) && ((cc >= num))) // SW
            {
                r = rr;
                c = cc;
                product = list[r++][c--];
                for (int i = 0; i < num; i++)
                {
                    product = list[r++][c--];
                }
                greatest_product = MAX(greatest_product, product);
            }
            if (((rr + num) <= row) && ((cc + num) <= column)) // SE
            {
                r = rr;
                c = cc;
                product = list[r++][c++];
                for (int i = 0; i < num; i++)
                {
                    product = list[r++][c++];
                }
                greatest_product = MAX(greatest_product, product);
            }
            //cout << "G: " << greatest_product << endl;
        }
    }
    cout << "Greatest Product: " << greatest_product << endl;
}

int main(void)
{
    largest_product_in_series(13);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

当我运行你的代码时,它表示列是50,行是21.这似乎有点奇怪,因为它应该是一个20x50阵列,而不是一个21x50阵列,这将是一个1050位数字。最好的猜测是,你不小心在第21行存储了一些不可打印的字符,产品涉及其中的一些。

此外,部分循环有product = ...而不是product *= ...

最后,它不会影响答案,但乘法是可交换的,所以如果你得到一些前后数字的乘积,结果将是相同的。所以你可以通过N xor W去除所有方向来简化你的解决方案。