This commit is contained in:
2025-03-26 11:58:36 +08:00
parent a09362480a
commit 93f367dee9
3 changed files with 108 additions and 0 deletions

52
src/2974.c Normal file
View File

@@ -0,0 +1,52 @@
//
// Created by xfj12 on 2025/3/25.
//
#include <solution/2974.h>
#include <stdio.h>
#include <stdlib.h>
int findMinElement(int *nums, int numsSize, int *returnIndex)
{
int min = nums[0];
*returnIndex = 0;
for (int i = 1; i < numsSize; ++i)
{
if (nums[i] < min)
{
min = nums[i];
*returnIndex = i;
}
}
return min;
}
void removeElement(int *nums, int numsSize, int idx)
{
for (int i = idx; i < numsSize - 1; ++i)
nums[i] = nums[i + 1];
}
int *numberGame(int *nums, int numsSize, int *returnSize)
{
int *result = malloc(sizeof(int) * numsSize);
*returnSize = 0;
while (numsSize > 0)
{
int aliceIndex;
const int alice = findMinElement(nums, numsSize, &aliceIndex);
removeElement(nums, numsSize--, aliceIndex);
int bobIndex;
const int bob = findMinElement(nums, numsSize, &bobIndex);
removeElement(nums, numsSize--, bobIndex);
result[(*returnSize)++] = bob;
result[(*returnSize)++] = alice;
}
for (int i = 0; i < numsSize; ++i)
printf("%d ", result[i]);
return result;
}