指標_錯的_最後的元素

 avatar
user_3763047219
c_cpp
a year ago
1.6 kB
0
Indexable
Never
int extractMax(int* stone, int stoneSize);
void insert(int* stone, int stoneSize, int value);
int lastStoneWeight(int* stones, int stonesSize);


int extractMax(int* stone, int stoneSize) {
    if (stoneSize > 1) {
        insert(stone, stoneSize, *stone);
    }
    else if (stoneSize == 1) {
        return *stone;
    }
    else if (stoneSize == 0) {
        return 0;
    }
}

void insert(int* stone, int stoneSize, int value) {
    if (value == *(stone-1)) {
        *stone = 0;
        *(stone - 1) = 0;
        stoneSize =stoneSize- 2;
        stone = stone - 2;
    }
    else {
        *(stone - 1) = value;
        *stone = 0;
        stoneSize = stoneSize- 1;
        stone = stone - 1;
    }
    extractMax(stone, stoneSize);
}

int lastStoneWeight(int* stones, int stonesSize) {
    for (int i = stonesSize - 1; i > 0; i--) {
        for (int j = 0; j < i; j++) {
            if (*stones > *(stones + 1)) {
                int temp = *stones;
                *stones = *(stones + 1);
                *(stones + 1) = temp;
            }
            stones++;
        }
        stones = stones - i;
    }
    stones = stones + stonesSize - 1;

    return extractMax(stones, stonesSize);
}


#include <stdio.h>
#include <stdlib.h>


int main() {
    int n = 0;
    int ans;
    scanf("%d", &n);
    int* a = (int*)malloc(n * sizeof(int));

    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    ans = lastStoneWeight(a, n);
    printf("%d", ans);
    free(a);
    return 0;
}