2013-12-19 25 views
0

例如: 我有21个球的列表,我想分配它们在桶中。 桶大小应该是最小2和最大5.分割列表到给定的相等大小X和其余列表不应小于大小Y

I want output like 
B1 has 5 Balls 
B2 has 5 Balls 
B3 has 5 Balls 
B4 has 4 Balls 
B5 has 2 Balls 

这意味着欲铲斗分配给球的最大数量,然后剩余的。 我们可以采取无限制的桶。 如果可能,我想用JAVA代码。 请帮我找到解决办法 谢谢

+0

这是功课吗? –

+0

多少个水桶?总是5? –

+0

无限制的铲斗 –

回答

3

n/5会给你大小为5的桶满的数量。

n%5会给你其余的,不能放置。

如果n%5 >= 2,创建一个新的存储桶并放置它们。

如果n%5 = 1,创建一个新的桶,添加它,并在另一个桶中选择一个。

该实施例表明该算法:

public class Test { 

    public static void main(String[] args) {   
     place(21,5);   
    } 

    public static void place(int number, int sizeBucket){ 
     int nbBuckets = number/sizeBucket; 
     int nbLeft = number % sizeBucket; 
     List<Bucket> lBuckets = new ArrayList<>(); 
     for(int i = 0; i < nbBuckets; i++){ 
      lBuckets.add(new Bucket(sizeBucket, sizeBucket)); 
     }  
     if(nbLeft >= 2) 
      lBuckets.add(new Bucket(5, nbLeft)); 
     else if (nbLeft == 1){ 
      Bucket b = lBuckets.get(lBuckets.size()-1); 
      b.setSize(b.getSize()-1); 
      lBuckets.add(new Bucket(sizeBucket, nbLeft+1)); 
     } 
     System.out.println(lBuckets); 
    } 
} 

class Bucket { 
    private int capacity; 
    private int size; 

    public Bucket(int capacity, int size) { 
     super(); 
     this.capacity = capacity; 
     this.size = size; 
    } 

    public int getCapacity() { 
     return capacity; 
    } 

    public void setCapacity(int capacity) { 
     this.capacity = capacity; 
    } 

    public int getSize() { 
     return size; 
    } 

    public void setSize(int size) { 
     this.size = size; 
    } 

    @Override 
    public String toString() { 
     return "Bucket [capacity=" + capacity + ", size=" + size + "]"; 
    }  
} 

输出:

[Bucket [capacity=5, size=5], Bucket [capacity=5, size=5], Bucket [capacity=5, size=5], Bucket [capacity=5, size=4], Bucket [capacity=5, size=2]] 
+0

没有球,铲斗最大和最小尺寸可能会发生变化? –

+0

@OomphFortuity你是什么意思? – user2336315

+0

我得到了逻辑..谢谢 –

2

1.检查是否填充一个桶5个球后留下至少两个球。
2.如果没有,只要尝试用4等和so..till 2(如果是2,那将是你的最后一个桶)
3.填写一个水桶
4.重复这些步骤你有足够的球

0

我设计了一个通用的方法,你在哪里ü可以进入分钟,球的最大尺寸(桶)和球的总数

方法来获取桶的数量要创建。

long getNoOfBucket(long total, int max, int min) { 
     if (total % max < min && total % max != 0) { 
      return (total/max) + 1; 
     } else { 
      return total/max; 
     } 

方法来获取桶的列表。

List<List<Long>> getbucketOfBuckets(long total, int max, int min) { 
      List<Long> tempBucket = null; 
      List<List<Long>> parentbucket = new ArrayList<List<Long>>(); 
      if (total % max < min && total % max != 0) { 
       long noOfBucket = getNoOfBucket(total, max, min); 
       for (long i = noOfBucket; i >= 1;i--) { 
        if (total % max < min && total % max !=0) { 
         total = total - min; 
         long tempVar = total+1; 
         tempBucket = new ArrayList<Long>(); 

         for (long j = 1; j <= min; j++) { 

          tempBucket.add(tempVar); 
          tempVar++; 
         } 
         parentbucket.add(tempBucket); 
        } else { 
         long tempmax = 0; 
         if(total%max == 0){ 
          tempmax = max; 
         }else{ 
          tempmax = total%max; 
         } 
         total = total - tempmax; 
         long tempVar = total+1; 
         tempBucket = new ArrayList<Long>(); 
         for (int j = 1; j <= tempmax; j++) { 

          tempBucket.add(tempVar); 
          tempVar++; 
         } 
         parentbucket.add(tempBucket); 
        } 
       } 
      } 
else{ 
      for (long i = noOfBucket; i >= 1; i--) { 

       total = total - max; 
       long tempVar = total + 1; 
       tempBucket = new ArrayList<Long>(); 
       for (int j = 1; j <= max; j++) { 

        tempBucket.add(tempVar); 
        tempVar++; 
       } 
       parentbucket.add(tempBucket); 
      } 
      } 
      return parentbucket; 

主要的方法来测试上面的代码

public static void main(String[] args) { 
    long total = Long.valueOf(args[0]); 
    int max = Integer.valueOf(args[1]); 
    int min = Integer.valueOf(args[2]); 
    long noOfBucket = BallServicesImplementation.getNoOfBucket(total, max,min); 

    List<List<Long>> parentBuclet = BallServicesImplementation.getbucketOfBuckets(total, max, min); 
    for(List<Long> itr : parentBuclet){ 
     System.out.println("size :: "+itr.size()); 
     for(Long seLong : itr){ 
      System.out.print(seLong+"\t"); 
     } 
     System.out.println(""); 
     System.out.println("********************************************"); 
    } 

我测试不同的测试cases.u下可以尝试,让我知道,如果需要一些更多的改进。希望它能帮助你。

+0

尼斯..正常工作... –