1103. 分糖果 II - 力扣(LeetCode)
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* distributeCandies(int candies, int num_people, int* returnSize)
{
int num = 0;
int* arr = (int*)malloc(sizeof(int)*num_people);
for(int i = 0; i < num_people; ++i)//赋初值为0
arr[i] = 0;
*returnSize = num_people;
while(candies >= 0)
{
if(num + 1 <= candies)//够糖果给
{
arr[num % num_people] += num + 1;
candies -= num;
++num;
}
else//不够糖果给
arr[num % num_people] += candies;
}
return arr;
}