我有一个编写maxSubArray算法的任务。它大部分都在工作,但我在返回结构时遇到了一些问题。
以下是相关文件(对不起,文字墙,但我不确定如何指出此错误):
main.cpp
#include <iostream>
#include "./MaxSubarray.h"
using namespace std;
#define TEST(test) {
testNum++;
if (!(test)) {
cerr << "Test " << testNum << " failed" << endl;
numFails++;
}
}
int runTests() {
int numFails = 0;
int testNum = 0;
{
// 0 1 2* 3 4 5
int A[] = { 1, -4, 14, -2, 3, -1 };
Result r = findMaxCrossingSubarray(A, 0, 2, 5);
Result c(2, 4, 15);
TEST(r == c);
}
{
// 0 1 2 3* 4 5 6
int A[] = { 0, 5, -4, 1, -2, -3, 6 };
Result r = findMaxCrossingSubarray(A, 0, 3, 6);
Result c(1, 6, 3);
TEST(r == c);
}
{
// 0 1 2 3* 4 5 6
int A[] = { 0, 5, -4, 1, -2, -3, 5 };
Result r = findMaxCrossingSubarray(A, 0, 3, 6);
Result c(1, 6, 2);
TEST(r == c);
}
{
int A[] = { 13, -3, 4 };
Result r = findMaxSubarray(A, 0, 2);
Result c(0, 2, 14);
TEST(r == c);
}
{
int A[] = { 13, 4, -3 };
Result r = findMaxSubarray(A, 0, 2);
Result c(0, 1, 17);
TEST(r == c);
}
{
int A[] = { -3, 4, 13 };
Result r = findMaxSubarray(A, 0, 2);
Result c(1, 2, 17);
TEST(r == c);
}
{
int A[] = { 4, -3, 13 };
Result r = findMaxSubarray(A, 0, 2);
Result c(0, 2, 14);
TEST(r == c);
}
{
int A[] = { 4, -3, -13, 5, 3 };
Result r = findMaxSubarray(A, 0, 4);
Result c(3, 4, 8);
TEST(r == c);
}
{
int A[] = { 4, 3, -13, -5, 3 };
Result r = findMaxSubarray(A, 0, 4);
Result c(0, 1, 7);
TEST(r == c);
}
{
int A[] = { -4, 4, -3, 5, -3 };
Result r = findMaxSubarray(A, 0, 4);
Result c(1, 3, 6);
TEST(r == c);
}
{
int A[] = { 13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7 };
Result r = findMaxSubarray(A, 0, 15);
Result c(7, 10, 43);
TEST(r == c);
}
const int numSuccesses = testNum - numFails;
cout << numSuccesses << "/" << testNum << " tests succeeded" << endl;
return numFails;
}
int main() {
// TODO: Add test code as necessary.
// This file will NOT be submitted, though!
return runTests();
}
MaxSubarray.cpp
#include "./MaxSubarray.h"
#include <iostream>
// Provides floor, ceil, etc.
#include <cmath>
#include <climits>
using namespace std;
//Kalen Williams
//27 January 2017
Result findMaxCrossingSubarray(int* array, int low, int mid, int high){
int leftSum = INT_MIN;
int sum = 0;
int maxLeftIndex;
for(int i = mid; i >= low; i--){
sum = sum + array[i];
if(sum > leftSum){
leftSum = sum;
maxLeftIndex = i;
}
}
int rightSum = INT_MIN;
sum = 0;
int maxRightIndex;
for(int j = mid + 1; j <= high; j++){
sum = sum + array[j];
if(sum > rightSum){
rightSum = sum;
maxRightIndex = j;
}
}
int totalSum = leftSum + rightSum;
return Result(maxLeftIndex, maxRightIndex, totalSum);
}
Result findMaxSubarray(int* array, int low, int high){
if(high = low){
return Result(low, high, array[low]);
}
else{
int mid = (low + high) / 2;
//
Result leftArray = findMaxSubarray(array, low, mid);
Result rightArray = findMaxSubarray(array, mid + 1, high);
Result crossArray = findMaxCrossingSubarray(array, low, mid, high);
if(leftArray.sum >= rightArray.sum && leftArray.sum >= crossArray.sum){
return leftArray;
}
else if(rightArray.sum >= leftArray.sum && rightArray.sum >= crossArray.sum){
return rightArray;
}
else{
return crossArray;
}
//
}
}
代码按原样运行,我通过了前 3 个测试,因为我的findMaxCrossingSubarray
有效,但是,当我取消注释代码时findMaxSubArray
出现错误
分段故障(核心转储)
我对这个问题做了相当多的研究,知道这意味着我正在尝试引用尚未分配给程序的内存,我只是不确定如何缩小问题范围。我尝试使用 -Wall 进行编译,但这给了我一堆各种错误,这些错误似乎都与此无关。
如果没有看到头文件,我无法确定这是导致 Seg 错误的原因,但您在findMaxSubarray
的第一行有一个错误:
if (high = low) {
你显然是说high == low
.您应该已经收到一些编译器警告。如果你是一个"常量纳粹",编译器会抓住这个......(即和错误而不是警告):我的意思是,当然,将const int high
和const int low
放在函数定义中(声明中忽略了const
,顺便说一句)。