公司有1000个苹果,和10个箱子,事先将1000个苹果分别装入10个箱子后,当客户无论需要多少苹果,都可以整箱整箱的提供给客户。(java解决)

解决方案 »

  1.   

    面试的时候也碰到过这样的题目,有最少的砝码表示1—7克的重量?treeroot(旗鲁特) 和88324877(看懂请给分,你好我也好。) 的回答是正确的
      

  2.   

    http://hi.baidu.com/boshiclub
    我们一起创业,我们一起发财,我们一起快乐,我们一起努力!
      

  3.   

    2^0 --- 2^9
    10个箱子分别放2^n(0<=n<=9),能够完全满足条件
      

  4.   

    public class test {
    private int m = 1000;//苹果个数
    private  int n = 10;//箱子个数
    private int[] result= new int[n];
    public static void main(String[] args) {
    new test().start();
    }
    public void start(){
    result[0] = 1;
    System.out.print("begin.....");
    for(int b=1;b<n;b++){
    for(int i=0;i<b;i++){
    result[b]=result[b]+result[i];
    }
    result[b]=result[b]+1;
    if((2*result[b]+1)>m){
    result[b]=m-result[b]+1;
    }else{

    }

    }
    for(int b=0;b<n;b++){
    System.out.print(result[b]);
    }
    }
    }
      

  5.   

    jeremyyang0824() 
    的答案,我顶
      

  6.   

    1   2   4    8  16  32   64    128   256   512正解答案只有这一个,道理很简单,第N只箱子应该装的数量,应该是前面1到N-1只箱子装的数量的总和加1jeremyyang0824() 的答案太可笑了
      

  7.   

    上面的答案给我我点启发
    其实这个就像我们在定义C/C++中的移位宏一样
    #define a 0x1
    #dedine b 0x2
    #define c 0x4
    ....那么你要什么数目,你就可以 * | * |...这么来了
      

  8.   

    sorry
       
     study
      

  9.   

    j99616(斩风断雨) ( ) 信誉:100    Blog  2007-1-13 22:14:03  得分: 0  
     
     
       
    1   2   4    8  16  32   64    128   256   488
      
     
    //////////////////////////////////////////////
    你有什么权力笑话别人,看看你给的答案。是1000吗? 1+488 =499  剩下的都是偶数,那个苹果你吃了?
      

  10.   

    综合了各位的建议,小弟做了一下。public class Test{
    private int []appleBox; 

    public void putAppleToBox(int appleCount, int boxCount) {
    appleBox = new int[boxCount];
    for(int i = 0; i < boxCount; i++) {
    int temp = (int)Math.pow(2, i); 
    if (appleCount >= temp) {
    appleBox[i] = temp;
    appleCount -= temp;
    } else {
    appleBox[i] = appleCount;
    }
    }
    }

    public String getApple(int appleCount){
    StringBuffer boxs = new StringBuffer();
    for (int i = 9; i >= 0; i--) {
    if (appleCount >= appleBox[i]) {
    appleCount -=  appleBox[i]; 
    boxs.append(i+1).append(" = ").append(appleBox[i]).append("\n");
    }
    }
    return boxs.toString(); 
    }
        public static void main(String[] args) {
         Test t = new Test(); 
         t.putAppleToBox(1000, 10); 
         String box = t.getApple(902); 
         System.out.println(box); 
        }
    }