/**
* 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;
}
每日一练——分糖果2
最新推荐文章于 2024-10-31 16:16:13 发布