2017-04-02 19 views
-1

で最大の製品検索:基本的に私は、2次元配列を通過し、8つの方向(N、NW、NEのそれぞれから13桁の製品を取得していますhttps://projecteuler.net/problem=8私は次のような問題を解決しようとしていますシリーズ

をE、W、S、SW、SE)、戻り値を最大値に設定します(存在する場合)。

しかし、私は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

おそらくあなたのバグではありませんが、while(!fin.eof())は修正する価値があります。詳細はこちら:[iostream :: eofがループ状態になっているのはなぜですか?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301

+1

あなたが間違っていることは非常に明白です。実行中にコードをステップ実行するためにデバッガを使用せず、1行ずつ実行し、すべての変数の値を調べて、プログラムの実際のロジックがどのように異なるかを判断しますあなたの期待される結果。デバッガの使い方を知ることは、すべてのC++開発者にとって必要なスキルです。あなた自身でこれを理解することができるはずです。これはまさにデバッガのためのものです。 –

答えて

0

私はあなたのコードを実行し、それは列が50あると言う、そしてそれは20x50でなければなりませんので、行は、少し奇妙に思える21.です:

以下は私の簡単な、非効率的なソリューションです。配列ではなく、2150の配列で、これは1050桁の数字です。最高の推測では、誤って印刷できない文字が21行目に格納されていて、その中にはいくつかの文字が含まれているということです。

また、ループの一部ではなくproduct *= ...

product = ...を持っているそして最後に、それが答えに影響を与えないだろうが、あなたは前後いくつかの数字の積を取得するので、もし乗算は、結果可換であります同じになります。だから、方向にN xor Wを使ってすべてを取り除くことによって、あなたのソリューションを少し簡略化することができます。

関連する問題