最大子数组蛮力法对第一个数组产生较大的总和
max subarray brute force method yields large sum for first array
我编写了一个代码,用于使用蛮力方法计算最大子数组。我的代码从输入文件读取多个数组,并返回包含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" 加起来以产生正确的结果。
在您提供的第一个示例中,所有数字都是正数。这意味着最大和子数组实际上是中所有数组元素的和。
你的算法部分没问题;
相关文章:
- 在c++中为double类型的数组创建一个unique_ptr
- 从数组构造一个跨度<shared_ptr<T>><shared_ptr<void>>
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- 如何将 v8::FunctionCallbackInfo<v8::Value> 数组从一个隔离复制到另一个隔离?
- 我怎么能有一个以2D数组为参数的函数,而数组有一个我想更改的参数/维度
- 为什么我的数组输出一个不在其中的元素
- 数组 - 最后一个索引
- 将数组作为一个参数从一个函数传递到C 中的另一个函数
- 从字符串数组创建一个字符数组
- 如何在C 中的小写中将文本从数组的一个维度传递到另一个维度
- 在对结构数组的一个成员进行排序后,移动其成员的其余部分
- 为什么数组在一个文件中打印,但在发送到另一个文件时却缺少数字
- C++,为什么多维数组的一个元素的增加似乎增加了另一个元素
- 将数组的一个元素与整个数组元素进行比较
- 如何将数组从一个函数传递到另一个函数
- C++:通过引用传递对象数组(指向一个?的指针)
- 查找最大和连续子数组-另一个版本
- 如何使用一个整数数组作为一个无序映射的键
- 使用for循环将char数组的一个元素复制到另一个元素
- C++如何将变量char数组从一个函数传递到另一个函数