で最大の製品検索:基本的に私は、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;
}
おそらくあなたのバグではありませんが、while(!fin.eof())は修正する価値があります。詳細はこちら:[iostream :: eofがループ状態になっているのはなぜですか?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301
あなたが間違っていることは非常に明白です。実行中にコードをステップ実行するためにデバッガを使用せず、1行ずつ実行し、すべての変数の値を調べて、プログラムの実際のロジックがどのように異なるかを判断しますあなたの期待される結果。デバッガの使い方を知ることは、すべてのC++開発者にとって必要なスキルです。あなた自身でこれを理解することができるはずです。これはまさにデバッガのためのものです。 –