想破头也没想出来算法15种商品
每种商品最便宜2元,最贵10元
15种商品的总价值必须是100元
且每种商品的价格浮动是1元(也就是说,每种商品只能是如下价格:2、3、4、5、6、7、8、9、10)满足上述条件,总共能有多少种组合呢
每种组合是什么?大虾们有啥算法,指教下在下
详细点哈,菜鸟菜鸟~~

解决方案 »

  1.   

    public class LieJu { public static void main(String[] args) {
    for(int a=2;a<=10;a++){
    for(int b=2;b<=10;b++){
    for(int c=2;c<=10;c++){
    for(int d=2;d<=10;d++){
    for(int e=2;e<=10;e++){
    for(int f=2;f<=10;f++){
    for(int g=2;g<=10;g++){
    for(int h=2;h<=10;h++){
    for(int i=2;i<=10;i++){
    for(int j=2;j<=10;j++){
    for(int k=2;k<=10;k++){
    for(int l=2;l<=10;l++){
    for(int m=2;m<=10;m++){
    for(int n=2;n<=10;n++){
    for(int o=2;o<=10;o++){
    if(a+b+c+d+e+f+g+h+i+j+k+l+m+n+o==100)System.out.println(a+" "+ b+" "+c+" "+d+ " "+e+" "+f+" "+g+" "+h+" "+i+" "+j+" "+k+" "+l+" "+m+" "+n+" "+o);

    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }

    } }}枚举法(就是效率差点)。每一行的15个数字代表每一个商品的价钱
      

  2.   


    public class PriceArrange {
    public static List<List<Integer>> getArrange(int[] is, int begin, int sum) {
    List<List<Integer>> result = new ArrayList<List<Integer>>();
    if (sum==0) result.add(new ArrayList<Integer>());
    if (begin>=is.length || sum<=0) return result;
    List<List<Integer>> l1 = getArrange(is, begin+1, sum-is[begin]);
    for (int i=0; i<l1.size(); i++) l1.get(i).add(0, is[begin]);
    result.addAll(l1);
    result.addAll(getArrange(is, begin+1, sum));
    return result;
    }

    public static void main(String[] args) {
    int[] price = {3,4,8,9,5,7,6,6,8,10,5,8,10,9,10};
    List<List<Integer>> lprice = getArrange(price,0,100);
    System.out.println(lprice.size() + "种组合:");
    for (int i=0; i<lprice.size(); i++)
    System.out.println(lprice.get(i));
    }
    }
      

  3.   


    final int COUNT = 15;//15个数
    int[] nums = new int[COUNT];//核心数组//赋初值
    for(int i = 0; i < COUNT; i++){
        nums[i] = 2;//先假设所有的商品都是价格最低的
    }int p = 0;//游标//穷举所有组合
    while(true){    //这个位置上把数组里所有数相加看看是不是等于100,该输出就输出,我在这里就不写代码了
        //下面这段代码的大概意思是,让某一位上的价格加1,如果加到10了,下一步就该让下一位加1当前位置为初始,(可以理解为进位,个位加到9了,再要加的话就让十位加1,个位置0)
        nums[p]++;
        if(nums[p] > 10){
           nums[p] = 2;//置为初值,因为最小的价格是2.
           p++;//让游标指向下一位,
        }else{
           p = 0;
        }}
      

  4.   

    public class Test
    {
    public static void main(String[] args)
    {
    int[] goods = new int[15];
    for (int i = 0; i < goods.length; i++)
    {
    goods[i] = 2;
    }
    for (int i = 0; i < 15; i++)
    {
    for (int j = 2; j <= 10; j++)
    {
    goods[i] = j;
    if(countArray(goods) == 100)
    {
    isCount(goods);
    }
    }
    }
    }

    public static int countArray(int[] goods)
    {
    int count = 0;
    for (int i = 0; i < goods.length; i++)
    {
    count += goods[i];
    }
    return count;
    }

    public static void isCount(int[] goods)
    {
    for (int i = 0; i < goods.length; i++)
    {
    System.out.print(goods[i] + "    ");
    }
    System.out.println();
    }
    }
    如有不对还望各位指出