❔ (Beginner) Seeking Advice on Distributing Items Equally in C# Program
Hey everyone! I hope you're doing well. I'm still pretty new around here and not exactly a pro in the world of C#, but I love programming in my spare time. A few weeks back, I started with C# and now I'm working on this project for an app.
I'm tinkering with a program that evenly distributes different things (like 3 tomatoes, 4 bananas, and 6 apples) among a set number of bags (let's say 3 bags). The total count of each type of fruit and the overall count of items shouldn't differ by more than one, so it's as fair as possible for each bag.
My approach is to create a list of integer arrays, which I'd call 'bags.' Each part of this list would hold an integer array where the items for each bag would go. Do you see where I'm going with this? I could really use your help and would appreciate your thoughts on how best to proceed. Should I divide the count of items or what would be your approach? Looking forward to your suggestions! Thanks in advance.
3 Replies
Dividing the number of items by the number of bags seems like a fine way to go about it
Then you would round the result of that division down for
n - 1
items, and up for one item
Or... no, forget that last bit
It wouldn't always work
You'd divide the number of items by the number of bags, round that down and sum up the decimal parts of all those numbers. Then you'd distribute that, one per bag until it's goneAre the rounded numbers correctly stored in the 'results' array and the remainders, i.e., the decimal remainders, in the 'sumUp' array? Now, the only thing left is the distribution, but is this approach correct?" Code:
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.