排序后数组大小发生变化

Array size changed after sorting

我目前正在做一项作业,该作业要求我获取数组 (ex: arr[] = {1,2,3,4,5}, valid sequence is {1,2},{2,3},{5}, or {2,3,4,5} 中序列的最大计数。我使用了一种算法,它在不排序的情况下找到数组的最大值,但是,在线判断认为它是错误的,因为它 运行 太长了(Time Limit Error)。所以我更改了我的代码以使用排序算法。

我试图通过首先对数组进行排序,然后打印数组的最后一个值(最大)来找到数组中的最大值,如果我输入以下内容,它就会起作用:

Input:
1 // cases
3 2
2 2 2
Output:
SIZE of Array is: 3
UNSORTED countArr:
0. 1
1. 1
2. 1
(after sorting) SORTED countArr:
0. 1
1. 1
2. 1

但是,如果我尝试输入多个 "cases",我会得到:

Input:
2 // cases
4 11
2 9 1 1
3 2
2 2 2
Output:
SIZE of Array is: 4
UNSORTED countArr:
0. 2
1. 3
2. 2
3. 1
(after sorting) SORTED countArr:
0. 1
1. 2
2. 2
3. 3
SIZE of Array is: 4  //why did the array size become 4, instead of 3
UNSORTED countArr:
0. 1
1. 1
2. 1
3. 3  // and what is this 3 doing here? it should have ended at number 2.
(after sorting) SORTED countArr:
0. 1
1. 1
2. 1
3. 3  // same as above

如果有人能帮忙,你能告诉我哪里错了吗? 源代码:

#include <stdio.h>

// all function is for quicksort
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 [5001], 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);
        for (j = 0; j < numofElement; j++) {
            scanf ("%d", &set [j]);
        }
        // so the program knows if the array 'set []' is reaching its last digit
        set [numofElement] = -2;
        for (k = 0; k < numofElement; k++) {
            if (set [k] > limit) {
                // to skip over or (if all sequence is invalid) to print "-1"
                countArr [k] = -1;
                continue;
            }
            for (l = k; l < numofElement; l++) {
                sum += set [l];
                count += 1;
                if ((sum <= limit) && (sum + set [l + 1] > limit || set [l + 1] == -2)) {
                    countArr [k] = count;
                    sum = 0;
                    count = 0;
                    break;
                }
            }
        }
        // count how many number there are in 'countArr []', so we can find its largest value
        size = 0;
        l = 0;
        while (countArr [l] != 0) {
            size += 1;
            l++;
        }
        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]);
        }
        // sort the 'temp []' array, and output its largest value
        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;
}

是一个简单的错误,你没有在第一个for循环开始时将countArr数组的元素重置为0。 如果您修复此问题,您的程序应该可以运行。

执行此指令后,您需要将重置添加到零:

for (i = 0; i < cases; i++){
... reset to zero countArr
... rest of the programm
}