本文主要是介绍每日一练——分糖果2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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)//赋初值为0arr[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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!