有1块,2块,5块,10块的钱可以任意组合,组合成100块钱,有几种组法?程序实现。最好循环在10000次以下。

解决方案 »

  1.   


    public class Money {
    public final static int TOTAL = 100;
    public final static int MONEY[] = new int[]{1,2,5,10};
    public static void main(String[] args) {
    int loopNum = 0;
    int sum = 0;
    for(int i = 0;i<=TOTAL/MONEY[3];i++){
    int last5 = (TOTAL - i * MONEY[3])/MONEY[2];
    for(int j = 0;j<=last5;j++){
    int last2 = ((last5 - j) * MONEY[2])/MONEY[1];
    for(int k = 0;k<=last2;k++){
    int last1 = ((last2 - k) * MONEY[1])/MONEY[0];
    loopNum++;
    if(last1>=0){
    sum++;
    }
    }
    }
    }
    System.out.println(loopNum);
    System.out.println(sum);
    }
    }
    大家看看,不知道对不对?做出来是2156
      

  2.   

    100=10*10  也就是说问题分解为10有多少种组合
    10=5*2 也就是说问题分解为5有多少种组合
    5=2*2+1 看来还是没有分解到最基础元素
    2=1*2 终于结束了,现在开始向前回溯2有(2种)分解方法2和1+1
    5有几种呢??!! 2*2则应该代表有(2种)的平方种,再加上5自身是(1种),就是(5种)
    10呢!!?? 5*2则代表有(5种)的平方种,再加上10自身是(1种),就是(26种) 
    100当然就是(26种)的10次方了(1.41167095653376E14种)楼上那位仁兄的代码我不敢说不对不过我觉得我的思想应该没有错,如果我是对的,那以下几行是不必要的
    int last5 = (TOTAL - i * MONEY[3])/MONEY[2];
    int last2 = ((last5 - j) * MONEY[2])/MONEY[1];
    int last1 = ((last2 - k) * MONEY[1])/MONEY[0];小学编程我想应该还没有楼上仁兄想得那么复杂,遵循以上思想用三个循环就应该可以了.
      

  3.   

    谢谢各位。我自己做的开始是:public static void main(String[] args) {
    // TODO Auto-generated method stub
    int count=0,a=0,b,c,d;
    long sum=0l;
    for(a=0;a<=10;a++)
    for(b=0;b<=(100-10*a)/5;b++)
    for(c=0;c<=(100-10*a-5*b)/2;c++)  //执行到这根本就不用考虑1的循环了,
    {                                 //因为(100-10*a-5*b)/2 不管结果是几1总有一种凑法补上  
                sum++;
                count++;
    }

                   System.out.println(count);
           System.out.println(sum);
                  }