思路
方法:暴力模拟
最直观的方法是不断地遍历数组,如果还有糖就一直分,直到没有糖为止。
1 class Solution { 2 public: 3 vector<int> distributeCandies(int candies, int num_people) { 4 vector<int> res(num_people, 0); 5 int x = 1; 6 int i = 0; 7 while(candies > x) { 8 res[i] += x; 9 candies -= x; 10 x++; 11 i++; 12 if(i == num_people) 13 i = 0; 14 } 15 16 res[i] += candies; 17 18 return res; 19 } 20 };