-1
我試圖解決以下問題,找到最大的產品:https://projecteuler.net/problem=8一系列
基本上我通過二維數組去,並從每個八個方向(N,NW,NE獲得13位產品, 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;
}
可能不是你的bug,但'while(!fin.eof())'值得修復。更多的信息在這裏:[爲什麼iostream :: eof裏面的循環條件被認爲是錯誤的](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301
你做錯了什麼是非常明顯的:你沒有使用調試器來執行你的代碼,而是一次執行一行代碼,並且檢查所有變量的值以確定程序的實際邏輯如何偏離您的預期結果。瞭解如何使用調試器是每個C++開發人員必備的技能。你應該能夠自己弄清楚這一點。這正是調試器的用途。 –