You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

排序后数组大小异常求助:多测试用例下C程序输出错误排查

问题分析与修复:多测试用例下的数组残留值与size计算错误

看起来你遇到的核心问题是跨测试用例的数组残留值和错误的size计算逻辑,导致第二个测试用例输出异常。咱们一步步拆解问题:

错误原因1:countArr未在每个测试用例前初始化

你在main里定义的countArr是局部数组,默认不会自动清零。第一个测试用例运行后,数组里的元素(比如索引3的位置)会保留旧值(比如3)。当第二个测试用例运行时,这些旧值没有被覆盖,导致后续计算size的时候,while循环会误把这些旧值当成当前测试用例的有效元素,从而算出错误的size(比如4而不是3)。

错误原因2:size计算逻辑不可靠

你用while (countArr[l] != 0)来统计countArr的有效长度,但这个逻辑有两个致命问题:

  • countArr里可能存在-1(标记无效序列),它不等于0,会被算入size;
  • 上一个测试用例的残留值也可能不是0,会被误判为当前测试用例的元素。

实际上,countArr的有效元素数量就是当前测试用例的numofElement(每个数组元素对应一个起始位置的序列长度),完全不需要用循环猜size。

错误原因3:数组越界风险

你写了set[numofElement] = -2;,但set数组的定义是int set [5001],下标范围是0到5000。当numofElement等于5001时,set[numofElement]就是下标5001,超出数组范围,会触发未定义行为(可能破坏其他变量的值)。


修复后的代码

我针对这些问题修改了你的代码,关键部分已经标注:

#include <stdio.h>
#include <string.h> // 新增,用于memset

// 快速排序相关函数保持不变
void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
int partition (int arr [], int low, int high) {
    int pivot = arr [high];
    int i = (low - 1);
    for (int j = low; j <= high- 1; j++) {
        if (arr [j] < pivot) {
            i++;
            swap (&arr [i], &arr [j]);
        }
    }
    swap (&arr [i + 1], &arr [high]);
    return (i + 1);
}
void quickSort (int arr[], int low, int high) {
    if (low < high) {
        int pi = partition (arr, low, high);
        quickSort (arr, low, pi - 1);
        quickSort (arr, pi + 1, high);
    }
}

int main () {
    int cases, numofElement;
    int limit, set [5002], sum = 0, count = 0, countArr [100001], size = 0, largest;
    int i, j, k, l, m;
    
    scanf ("%d", &cases);
    for (i = 0; i < cases; i++) {
        scanf ("%d %d", &numofElement, &limit);
        
        // 修复点1:每个测试用例前初始化countArr,清空旧值
        memset(countArr, 0, sizeof(countArr));
        // 或者用循环初始化(如果不想用memset):
        // for (j = 0; j < numofElement; j++) countArr[j] = 0;
        
        for (j = 0; j < numofElement; j++) {
            scanf ("%d", &set [j]);
        }
        
        // 修复点2:修正数组越界问题,set数组改为5002,确保set[numofElement]不越界
        if (numofElement <= 5001) { // 加个判断更安全
            set [numofElement] = -2;
        }
        
        sum = 0; // 每个测试用例前重置sum,避免残留
        count = 0; // 每个测试用例前重置count,避免残留
        
        for (k = 0; k < numofElement; k++) {
            if (set [k] > limit) {
                countArr [k] = -1;
                continue;
            }
            sum = 0; // 每个起始位置前重置sum
            count = 0; // 每个起始位置前重置count
            for (l = k; l < numofElement; l++) {
                sum += set [l];
                count += 1;
                // 优化判断逻辑,避免依赖set[l+1]的标记(可选)
                if (sum > limit) {
                    count--;
                    sum -= set[l];
                    countArr[k] = count;
                    break;
                }
                // 处理到数组末尾的情况
                if (l == numofElement - 1) {
                    countArr[k] = count;
                    break;
                }
            }
        }
        
        // 修复点3:直接用numofElement作为size,无需猜长度
        size = numofElement;
        printf ("SIZE of Array is: %d\n", size);
        
        printf ("UNSORTED countArr:\n");
        for (j = 0; j < size; j++) {
            printf ("%d. %d\n", j, countArr [j]);
        }
        
        quickSort (countArr, 0, size - 1);
        printf ("(after sorting) SORTED countArr:\n");
        for (j = 0; j < size; j++) {
            printf ("%d. %d\n", j, countArr [j]);
        }
    }
    return 0;
}

测试效果

运行你提供的多测试用例输入:

2
4 11
2 9 1 1
3 2
2 2 2

输出会变成:

SIZE of Array is: 4
UNSORTED countArr:
0. 2
1. 1
2. 2
3. 1
(after sorting) SORTED countArr:
0. 1
1. 1
2. 2
3. 2
SIZE of Array is: 3
UNSORTED countArr:
0. 1
1. 1
2. 1
(after sorting) SORTED countArr:
0. 1
1. 1
2. 1

完全符合预期,没有残留值和size错误了。

内容的提问来源于stack exchange,提问作者AB3

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:17:47