私のプログラムの目的は、可能なすべての変更ソリューションを特定の金額に出力することです。たとえば、
望ましい出力
Change: 9
[1, 1, 1, 1, 5]
[1, 1, 1, 1, 1, 1, 1, 1, 1]
(with 9 = $0.09) ただし、出力は少し異なります。出力は次のようになります。
私の出力
Change: 9
[1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 5]
[1, 1, 1, 5, 1]
[1, 1, 5, 1, 1]
[1, 5, 1, 1, 1]
[5, 1, 1, 1, 1]
ご覧のとおり、文字通りすべての可能な解決策を提供してくれます。私は上位2つの答えだけを気にします。より多くの金額が要求された場合、これが大きな問題になることは明らかです。ここに私の質問があります:私のコードに基づいて、それぞれ1つの組み合わせのみを表示するように修正するにはどうすればよいですか?
コード
import java.io.*;
import java.util.*;
import java.lang.*;
public class homework5 {
public static int change;
public static void main(String[] args)
throws FileNotFoundException { //begin main
ArrayList<Integer> coinTypes = new ArrayList<Integer>();//array to store
//coin types
ArrayList<Integer> answerCoins = new ArrayList<Integer>(); //to contain solutions
Integer i;
File f = new File (args[0]);
Scanner input = new Scanner(f); //initialize scanner
input.nextLine();
while(input.hasNextInt()) {
i = input.nextInt();
coinTypes.add(i); //add all ints to file
}
change = coinTypes.get(coinTypes.size()-1);
coinTypes.remove(coinTypes.size()-1);
System.out.println("Change: " + change);
findChange(change, coinTypes, answerCoins);
}
private static void findChange(int change, List<Integer> coinTypes,
List<Integer> answerCoins) { //contains means of
//finding the change solutions
if(change == 0) {
//base case
System.out.println(answerCoins);
}
else if(change < 0) {
//if negative it can't be a solution
} else {
for(int coin = 0; coin < coinTypes.size(); coin++) {
answerCoins.add(coinTypes.get(coin)); //choose
findChange(change-coinTypes.get(coin), coinTypes, answerCoins);//explore
answerCoins.remove(answerCoins.size()-1); //un-choose
}
}
}
}
すべての回答に感謝します。他の間違いを見逃すようにしてください。最初にこの質問に対処したいと思います。ありがとう!!