日期:2014-05-20  浏览次数:20745 次

一个计算问题
比如说A的价格是5,B的是3,C的是4,D的是2
我现在输入一个数,输入个10吧,怎么才能列出所有可能的情况,使得价格加起来为10

------解决方案--------------------
Java code

import java.util.Scanner;

public class Test {
    static class Things{
        String name;
        int price;
        
        public Things(String name,int price){
            this.name = name;
            this.price = price;
        }
    }
    
    static Things[] things = new Things[4];
    
    public static void main(String[] args) {
        things[0] = new Things("A",5);
        things[1] = new Things("B",3);
        things[2] = new Things("C",2);
        things[3] = new Things("D",4);
        for(int i = 0;i < things.length;i++){
            for(int j = i + 1;j < things.length;j++){
                if(things[j].price < things[i].price){
                    Things temp = things[j];
                    things[j] = things[i];
                    things[i] = temp;
                }
            }
        }
        Scanner scanner = new Scanner(System.in);
        int num = 0;
        System.out.print("输入总价格: ");
        try {
            num = scanner.nextInt();
        } catch (Exception e) {
            System.out.println("error");
        }
        StringBuffer result = new StringBuffer();
        for(int i = 0;i <= num / things[0].price;i++){
            if(things[0].price * i == num){
                result.append(things[0].name + " * " + i + "\n");
            }
            for(int j = 0;j <= num / things[1].price;j++){
                if(j != 0 &&things[0].price * i + things[1].price * j == num){
                    result.append(
                        things[0].name + " * " + i + " + " + 
                        things[1].name + " * " + j + "\n");
                }
                for(int m = 0;m <= num / things[2].price;m++){
                    if(m != 0 && things[0].price * i + things[1].price * j  + things[2].price * m == num){
                        result.append(
                            things[0].name + " * " + i + " + " +
                            things[1].name + " * " + j + " + " +
                            things[2].name + " * " + m + "\n");
                    }
                    for(int n = 0;n <= num / things[3].price;n++){
                        if(n != 0 && things[0].price * i + things[1].price * j  + 
                                things[2].price * m  + things[3].price * n == num){
                            result.append(
                                things[0].name + " * " + i + " + " +
                                things[1].name + " * " + j + " + " +
                                things[2].name + " * " + m + " + " +
                                things[3].name + " * " + n + "\n");
                        }
                    }
                }
            }
        }
        System.out.println(result);
    }

}

------解决方案--------------------
Java code
    public static void test4(int[] array, int begin, int tempVal, int val,List<Integer> result) {
        if (tempVal == val) {
            System.out.println(result);
            return;
        }
        for (int i = begin; i < array.length; i++) {
            if (tempVal < val) {
                result.add(array[i]);
                tempVal += array[i];
                test4(array, i + 1, tempVal, val, result);
                tempVal -= result.remove(result.size() - 1);
            }
        }
    }

    public static void main(String[] args) {
        test4(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 0, 0, 10,new ArrayList());
    }

------解决方案--------------------