一些上下文: 我正在解决HackerRank中关于连续子序列和一系列整数上具有最大和的子集的问题。该算法可能是正确的,我不是对它的正确性有所保留,而是我程序的异常行为,请继续阅读。
input.txt中:
1
6
2 -1 2 3 4 -5
输入格式:
number of testcases
number of integers (let that be n)
n space-sepearated integers
正确输出(基于来源):
10 11
实际输出:
11 11
如您所见,实际输出与正确的输出不匹配。我花时间在编写程序之前和之后使用笔和纸手动执行输入算法,我的计算似乎与正确的输出匹配(我的算法可能仍然不正确)。但之后,我尝试使用gdb逐行跟踪程序,并且在函数调用bestSum1的开始时发现了一些异常。
GDB输出代码段 :(转到我打印整数[0]的末尾附近的部分,并显示当我将其传递给函数时值已更改。
(gdb) break main
Breakpoint 1 at 0x4005fe: file maxsub.c, line 14.
(gdb) break bestSum1
Breakpoint 2 at 0x400745: file maxsub.c, line 33.
(gdb) run < input.txt
Starting program: /home/ken/Desktop/dynamic prog/maxsub < input.txt
Breakpoint 1, main () at maxsub.c:14
14 scanf("%d", &T);
(gdb) n
16 for (i = 0; i < T; i++) { // Each testcase
(gdb) n
17 scanf("%d", &N); // Get size of array
(gdb) n
18 int integers[N]; // Allocate memory
(gdb) n
20 for (j = 0; j < N; j++) { // Fill up array
(gdb) n
21 scanf("%d", &integers[j]);
(gdb) n
< SNIPPED PART > this is basically where for loop repeats N times
(gdb) n
20 for (j = 0; j < N; j++) { // Fill up array
(gdb) n
24 printf("%d %d\n", bestSum1(N, integers), bestSum2(N, integers)); // Print Answer
/** START TAKING A LOOK HERE, this is where the unusual thing happens: **/
(gdb) print integers[0] <---- INITIAL VALUE
$1 = 2
(gdb) print integers <---- ADDRESS OF ARRAY
$2 = 0x7fffffffdea0
(gdb) n
Breakpoint 2, bestSum1 (size=6, integers=0x7fffffffdea0) at maxsub.c:33
33 best_sum = - INFINITY, // Best Sum Found
(gdb) print integers[0] <---- VALUE CHANGED
$3 = -5
(gdb) print integers <---- SAME ADDRESS
$4 = (int *) 0x7fffffffdea0
(gdb)
完整源代码:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define INFINITY INT_MAX
int bestSum1(int size, int integers[size]);
int bestSum2(int size, int integers[size]);
int compare(const void* p1, const void* p2);
int main() {
int T, N, // testcases (T) and array size (N)
i, j; // Declare loop counters
scanf("%d", &T);
for (i = 0; i < T; i++) { // Each testcase
scanf("%d", &N); // Get size of array
int integers[N]; // Allocate memory
for (j = 0; j < N; j++) { // Fill up array
scanf("%d", &integers[j]);
}
// bestSum1 gets strictly contiguous subsequence with largest sum
// bestSum2 gets subset of numbers with largest sum
printf("%d %d\n", bestSum1(N, integers), bestSum2(N, integers));
}
return 0;
}
int bestSum1(int size, int integers[size]) {
// CONTIGUOUS SUBSEQUENCE WITH LARGEST SUM
int curr_sum = 0, // Current Sum
best_sum = - INFINITY, // Best Sum Found
value, // Where we store value
i; // Loop Counter
for (i = 0; i < size; i++) {
value = curr_sum + integers[i];
if (value <= 0) {
curr_sum = 0;
} else if (value > best_sum) {
best_sum = value;
curr_sum = best_sum;
}
}
return best_sum;
}
int bestSum2(int size, int integers[size]) {
// NON-CONTIGUOUS SUBSEQUENCE WITH LARGEST SUM
qsort(integers, size, sizeof(int), compare);
int sum = -1,
i;
for (i = 0; i < size; i++) {
if (integers[i] > 0) {
sum += integers[i];
}
}
return sum == -1 ? integers[size - 1] : sum;
}
int compare(const void* p1, const void* p2) {
return *((int*) p1) - *((int*) p2);
}
所以我想知道如何解决这个问题以及为什么会这样。