Evenly divide in c#

前端 未结 6 1737
梦谈多话
梦谈多话 2020-12-10 18:23

In c# how do I evenly divide 100 into 7?

So the result would be

  1. 16
  2. 14
  3. 14
  4. 14
  5. 14
  6. 14
  7. 14
6条回答
  •  醉酒成梦
    2020-12-10 18:54

    Not sure if this is exactly what you are after, but I would think that if you use Math.ceiling you will always end up with too big a total. Math.floor would underestimate and leave you with a difference that can be added to one of your pieces as you see fit.

    For example by this method you might end up with 7 lots of 14 giving you a remainder of 2. You can then either put this 2 into one of your pieces giving you the answer you suggested, or you could split it out evenly and add get two pieces of 15 (as suggested in one of the comments)

提交回复
热议问题