今天遇到了一个客户的需求是这样的:
有一个商场商品的清单列表,比如说是
冰红茶 0.9/瓶,绿茶0.9/瓶,矿泉水 0.85/瓶,可乐 1.7/瓶等等等。
他想从这些商品列表中选取几种商品分别乘以各个的瓶数,得出9000元。
如他要选冰红茶 可乐 矿泉水凑出9000的话,公式就是0.9x+0.85y+1.7z=9000.
需要给出x y z的的所有值(可以肯定x y z必须是整数)。从而可以引申到,从m个数中取出n个数(m>n),在给出一个目标值Q后,算出所有的因子x y z....的值,得到n1*x+n2*y+n3*z......=Q。
我想了想,只能用穷举,大家有什么办法啊?
欢迎大家讨论。

解决方案 »

  1.   

    呵呵,下面是我写的程序不知道如何:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    import java.util.StringTokenizer;
    import java.util.Vector;public class ForMyLover {
    //目标数
    private static  int TARGETNUM;
    //存放源数据
    private static Vector SRCARR = new Vector();
    private static File file = new File("./result.txt");
    private static FileOutputStream fops;
    private static String outPutString = "";
    private static int incFlag;
    public static void main(String args[]){
    Properties pros = new Properties();
    try{
    fops = new FileOutputStream(file);
    }catch(FileNotFoundException fnf){
    fnf.printStackTrace();
    }
    ClassLoader cl =  ForMyLover.class.getClassLoader();
    String _temStr ="";
    try {
    pros.load(cl.getResourceAsStream("formylover.properties"));
    TARGETNUM = new Integer(pros.getProperty("targetnum")).intValue();
    _temStr = pros.getProperty("srcarray");
    incFlag = new Integer(pros.getProperty("incFlag")).intValue();
    } catch (IOException e) {
    e.printStackTrace();
    }

    StringTokenizer st = new StringTokenizer(_temStr,",");
    while(st.hasMoreTokens()){
    SRCARR.add(st.nextToken());
    }
    //目标值/原始值得出的最大数的数组
    int maxNumArr[] = new int[SRCARR.size()];
    //个数
    int countNumArr[] = new int[SRCARR.size()];

    for(int i=0;i<SRCARR.size();i++){
    //算出最大数
    maxNumArr[i]=(int) (TARGETNUM/(new Double((String)SRCARR.get(i)).doubleValue()));
    //将存放个数的数组清零
    countNumArr[i]=0;
    }
    //开始算数
    while(countNumArr[SRCARR.size()-1]!=maxNumArr[SRCARR.size()-1]){
    increaceNum(countNumArr,maxNumArr);
    }
    try {
    System.out.println("写入文件成功!");
    fops.close();
    } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }


    }
    private static void increaceNum(int countNumArr[],int maxNumArr[]){

    countNumArr[0]=incFlag+countNumArr[0];

    for(int i=0;i<SRCARR.size();i++){
    if(countNumArr[i]>maxNumArr[i]){
    countNumArr[i]=0;
    if(i==SRCARR.size()-1){
    return;
    }
    countNumArr[i+1]=countNumArr[i+1]+incFlag;
    }
    }
    if(countNumArr[countNumArr.length-1]<100||countNumArr[countNumArr.length-2]<100){
    return;
    }
    int temInt = 0;  
    for(int i=0;i<SRCARR.size();i++){
    temInt=(int) (temInt+(new Double((String)SRCARR.get(i)).doubleValue())*countNumArr[i]);
    }

    if(temInt==TARGETNUM){
    outPutString = "";
    for(int i=0;i<SRCARR.size();i++){
    outPutString =outPutString+" "+countNumArr[i];
    }
    try {
    fops.write((outPutString+"\n\t").getBytes());
    } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
    }
    }
    }
    propertier文件内容:
    targetnum=89900
    srcarray=49.6,71.4,30.4
    incFlag=1