Frage

Say I want to distribute y items to x buckets evenly. If x is a multiple of y this distribution will be even, if not I can end up with 0 items in each bucket. For ex:

For ex: I have 3 buckets and I want to distribute 2 items each. Since doing the division (2/3) will lead to 0 items per bucket. How can I achieve, a distribution of 1, 1, 0?

War es hilfreich?

Lösung

This type of thinking should work:

package sandbox;

public class Sandbox
{

    public static void main(String[] args)
    {
        int numBuckets = 12;
        int numItems = 34;

        int itemsPerBucket = (numItems / numBuckets);
        int remainingItems = (numItems % numBuckets);

        for (int i = 1; i <= numBuckets; i++)
        {
            int extra = (i <= remainingItems) ? 1:0;
            System.out.println("bucket " + i + " contains " + (itemsPerBucket + extra) + " items.");
        }
    }
}

The output of this:

bucket 1 contains 3 items.
bucket 2 contains 3 items.
bucket 3 contains 3 items.
bucket 4 contains 3 items.
bucket 5 contains 3 items.
bucket 6 contains 3 items.
bucket 7 contains 3 items.
bucket 8 contains 3 items.
bucket 9 contains 3 items.
bucket 10 contains 3 items.
bucket 11 contains 2 items.
bucket 12 contains 2 items.

Notice the only looping you do is to talk about each bucket. You can easily just ask a bucket number and see how many items are in it without loop!

Andere Tipps

The first y mod x buckets will have (y div x) + 1 items, the rest will have y div x items

Each bucket gets y/x (integer division) items, and y % x buckets get 1 additional item.

Your question is kind vague, but taking the remainder from the division of the itemNumber (if they're in a list or array this could be the index) and the number of buckets as the index for the bucket would give you the even distribution you're looking for.

int bucketIndex = itemNumber % numberOfBuckets; 

It looks you know how to solve programming issues but you're lookin for an idea, i think your answer in pascal's triangle, think a little different than pascal's triangle, put your whole items into the top cell of the triangle... so, actually, i think thats not the best effort

Here is a recursive solution, just for fun.

import java.util.Arrays;

public class PizzaDistributor {

  public int[] distributePizzaToEatersEvenly(int numSlices, int[] eaters) {
    if (eaters == null || eaters.length == 0) {
      return null;
    }
    if (eaters.length > numSlices) {
      for (int i = 0; i < numSlices; i++) {
        eaters[i] += 1;
      }
    } else {
      int slices = numSlices / eaters.length;
      for (int i = 0; i < eaters.length; i++) {
        eaters[i] = slices;
      }
      //If there is a remainder to distribute, distribute it evenly, recursively.
      if ((numSlices % eaters.length) > 0) {
        distributePizzaToEatersEvenly(numSlices % eaters.length, eaters);
      }
    }
    return eaters;
  }

  public static void main(String[] args){
    PizzaDistributor pizzaDistributor = new PizzaDistributor ();
    System.out.println("0 slices, 1 eater:" +Arrays.toString(pizzaDistributor.distributePizzaToEatersEvenly(0,new int[1])));
    System.out.println("0 slices, 2 eaters:" +Arrays.toString(pizzaDistributor.distributePizzaToEatersEvenly(0,new int[2])));
    System.out.println("0 slices, 0 eaters:" +Arrays.toString(pizzaDistributor.distributePizzaToEatersEvenly(0,new int[0])));
    System.out.println("10 slices, 0 eaters:"+Arrays.toString(pizzaDistributor.distributePizzaToEatersEvenly(10,new int[0])));
    System.out.println("10 slices, 1 eater:" +Arrays.toString(pizzaDistributor.distributePizzaToEatersEvenly(10,new int[1]))); //lucky guy!
    System.out.println("10 slices, 3 eaters:" +Arrays.toString(pizzaDistributor.distributePizzaToEatersEvenly(10,new int[3])));
    System.out.println("10 slices, 5 eaters:" +Arrays.toString(pizzaDistributor.distributePizzaToEatersEvenly(10,new int[5])));
    System.out.println("10 slices, 7 eaters:" +Arrays.toString(pizzaDistributor.distributePizzaToEatersEvenly(10,new int[7])));
    System.out.println("10 slices, 10 eaters:" +Arrays.toString(pizzaDistributor.distributePizzaToEatersEvenly(10,new int[10])));
    System.out.println("10 slices, 13 eaters:" +Arrays.toString(pizzaDistributor.distributePizzaToEatersEvenly(10,new int[13])));
    System.out.println("10 slices, 20 eaters:" +Arrays.toString(pizzaDistributor.distributePizzaToEatersEvenly(10,new int[20])));

  }

}

Produces this output:

0 slices, 1 eater:[0]
0 slices, 2 eaters:[0, 0]
0 slices, 0 eaters:null
10 slices, 0 eaters:null
10 slices, 1 eater:[10]
10 slices, 3 eaters:[4, 3, 3]
10 slices, 5 eaters:[2, 2, 2, 2, 2]
10 slices, 7 eaters:[2, 2, 2, 1, 1, 1, 1]
10 slices, 10 eaters:[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
10 slices, 13 eaters:[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0]
10 slices, 20 eaters:[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Process finished with exit code 0
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top