2013-12-19 24 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

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