最大子数组蛮力法对第一个数组产生较大的总和

max subarray brute force method yields large sum for first array

本文关键字:数组 第一个      更新时间:2023-10-16

我编写了一个代码,用于使用蛮力方法计算最大子数组。我的代码从输入文件读取多个数组,并返回包含max子数组和和值的输出文件。

一切正常,除了输出文件上的第一个max子数组总是在末尾包含一个非常大的数字,它被添加到总和值中。随后的子数组没有这个问题。我在这篇文章的末尾包含了一个例子。

我不知道我哪里出错了。任何帮助将非常感激!

下面是运行算法并将其输出到输出文件的函数:

void a1(int a[], int size, string filename){ 
//run algorithm 1
int sum = a[0], start = 0, end = 0;
for (int x = 0; x < size; x++) {
    int tempSum = 0;
    int y = x;
    while(y>=0){
        tempSum += a[y];
        if(tempSum>sum){
            sum=tempSum;
            start=y;
            end=x;
        }
        y--;
    }
}
//print results on file
ofstream output;
output.open(filename.c_str(), ios::out | ios::app);
output << "nMax sum array: ";
    for (int x = start; x <= end; x++) {
        output << a[x];
        if (x != end) output << ", ";
    }
output << "nMax sum value: " <<  sum << "n";
output.close();
} 

主文件:

int main() {
    int a[50];
    ifstream inputFile;
    string s;
    stringstream ss;
    string outputfile = "MSS_Results.txt";
//print title
ofstream output;
output.open(outputfile.c_str(), ios::out | ios::app);
output << "Algorithm 1:n";
output.close();
//read file and run a1
int size;
char c;
inputFile.open("MSS_Problems.txt");
while (!inputFile.eof()) {
    getline(inputFile, s);
    size = 0;
    ss << s;
    ss >> c;
    while (ss.rdbuf()->in_avail()) {
        ss >> a[size];
        size++;
        ss >> c;
        if (!ss.rdbuf()->in_avail() && c != ']') {
            ss.clear(); 
            getline(inputFile, s);
            ss << s;
        }
    }
    ss.clear();
    if (size > 0) a1(a, size, outputfile);
}
inputFile.close();
  return 0;
}

输入文件示例:

[1, 2, 4, -1, 4, -10, 4, -19, 18, -1, -3, -4, 11, 3, -20, 19, -33, 50, 66, -22, -4, -55, 91, 100, -102, 9, 10, 19, -10, 10, 11, 11, -10, -18, 50, 90]
[12, 12, 14, -88, -1, 45, 6, 8, -33, 2, 8, -9, -33, -8, -23, -77, -89, 1, 9, 10, 92, 87]
[565, 78, 33, 9, 10, 84, 71, -4, -22, -55, -10, 76, -9, -9, -11, 76, 89, 11, 10, -33, 9]
[2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]

输出文件示例:

Algorithm 1:
Max sum array: 50, 66, -22, -4, -55, 91, 100, -102, 9, 10, 19, -10, 10, 11, 11, -10, -18, 50, 90, 3897136
Max sum value: 3897432
Max sum array: 1, 9, 10, 92, 87, 91
Max sum value: 290
Max sum array: 565, 78, 33, 9, 10, 84, 71, -4, -22, -55, -10, 76, -9, -9, -11, 76, 89, 11, 10, -33, 9, 87
Max sum value: 1055
Max sum array: 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 11
Max sum value: 103

可以看到,对于第一个数组,有一个不属于原始数组的3897136。

如果我从输入中删除第一行,输入看起来像这样:

[12, 12, 14, -88, -1, 45, 6, 8, -33, 2, 8, -9, -33, -8, -23, -77, -89, 1, 9, 10, 92, 87]
[565, 78, 33, 9, 10, 84, 71, -4, -22, -55, -10, 76, -9, -9, -11, 76, 89, 11, 10, -33, 9]
[2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]

现在我的输出看起来像这样:

Algorithm 1:
Max sum array: 1, 9, 10, 92, 87, 624
Max sum value: 823
Max sum array: 565, 78, 33, 9, 10, 84, 71, -4, -22, -55, -10, 76, -9, -9, -11, 76, 89, 11, 10, -33, 9, 87
Max sum value: 1055
Max sum array: 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
Max sum value: 92

我初始化数组不正确,这就是为什么它有时在末尾给我一个垃圾数字。为了正确初始化,我只更改了

a[100];

a[100] = {0}

,这就解决了数组末尾数字异常大的问题。

然后,我将a[100] = {0}移到while循环中,代码在其中读取输入文件。这似乎解决了将错误元素读入数组的新问题。

最后未解决的问题:数组末尾为0。

解决后会更新

由于所有的问题都是寻找最大的子数组,因此需要将末尾的"large number" 加起来以产生正确的结果。

在您提供的第一个示例中,所有数字都是正数。这意味着最大和子数组实际上是中所有数组元素的和。

你的算法部分没问题;