Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@yohhoy

整数区間のほぼ均等分割

半開区間[0, N) を 整数m で均等分割(+端数分散)

for (int i = 0; i < m; i++) {
  int r0 = N/m *  i    + std::min(N%m, i);
  int r1 = N/m * (i+1) + std::min(N%m, i+1);
  // i番目の半開区間[r0, r1)
}

計算例

  • N=10/m=2: [0,5), [5,10)
  • N=10/m=3: [0,4), [4,7), [7,10)
  • N=10/m=4: [0,3), [3,6), [6,8), [8,10)
  • N=3/m=5: [0,1), [1,2), [2,3), [3,3), [3,3)
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
3
Help us understand the problem. What are the problem?