我正在编写这个程序来解决以下等式: a x+b y+c z+....+n k <= d a, b, c, ..., n 和 d 将由用户输入。
想象 a, b, c, ..., n 是价目表,xy z... k 是数量。给你一笔 d 的钱,你需要优化你可以购买的好数量,直到你不能用剩下的东西买任何东西。
我的程序运行良好,少量良好(5)。如果我输入以下值: - 商品数量:6 - 价目表:- 第 1 项:600 - 第 2 项:900 - 第 3 项:1200 - 第 4 项:1600 - 第 5 项:1800 - 第 6 项:2400 - 总计:6000
,会导致“AWT-EventQueue-0”java.lang.StackOverflowError。
你能帮忙检查一下并建议我在哪里可以改进它吗?
btnNhpLngHng.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
isValid = true;
isValid = validateField();
if (!isValid)
JOptionPane.showMessageDialog(null, "Please input valid data");
else
goodQuantity = Integer.parseInt(goodQuantityTF.getText());
headerList = new Vector<String>(goodQuantity);
priceList = new Vector<Long>(goodQuantity);
quantityList = new Vector<Long>(goodQuantity);
isChecked = new Vector<Boolean>(goodQuantity);
for (int i = 1; i <= goodQuantity; i++) {
headerList.add("Mặt hàng " + i);
isChecked.add(false);
quantityList.add((long) 0);
}
for (int i = 0; i < goodQuantity; i++) {
vitrihang = i;
gianhap = Long.parseLong(JOptionPane.showInputDialog("Please input the price for good no." + (vitrihang+1)));
priceList.add(gianhap);
}
expectedSum = Long.parseLong(JOptionPane.showInputDialog("Please input expected total amount: "));
}
});
btnKimTra.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
for (int x1 = goodQuantity - 1; x1 > -1; x1--) {
inProcess = x1;
isDone = false;
timNghiem(x1);
}
}
});
}
public boolean validateField() {
if (goodQuantityTF.getText() == null)
return false;
else
try {
Double n = Double.parseDouble(goodQuantityTF.getText());
}
catch(Exception e) {
return false;
}
return true;
}
public void timNghiem(int a) {
//System.out.println(a);
if ((a+1) == goodQuantity) {
kiemNghiem();
isChecked.set(a, true);
if ((inProcess + 1) == goodQuantity) {
return;
} else {
timNghiem(a-1);
if (isChecked.get(inProcess)) {
isDone = true;
return;
}
}
} else {
if (!isChecked.get(a)) {
if (quantityList.get(a) <= (expectedSum/priceList.get(a))) {
tangNghiem(a);
kiemNghiem();
resetIsChecked(a);
timNghiem(a+1);
if (isChecked.get(inProcess)) {
isDone = true;
return;
}
} else {
isChecked.set(a, true);
if (isChecked.get(inProcess)) {
isDone = true;
return;
} else {
for (int x2 = inProcess; x2 < goodQuantity; x2++) {
if (!isChecked.get(x2)) {
if (isChecked.get(x2+1)) {
timNghiem(x2);
}
}
}
}
}
}
}
}
public void kiemNghiem() {
quantityList.set(goodQuantity-1, (long) 0);
do {
currentSum = 0;
for (int n = 0; n < goodQuantity; n++) {
currentSum = currentSum + priceList.get(n)*quantityList.get(n);
//System.out.println(priceList.get(n)*quantityList.get(n));
}
if (currentSum <= expectedSum) {
leftOver = expectedSum - currentSum;
isResult = true;
for (int n = 0; n< goodQuantity; n++) {
if (leftOver > priceList.get(n)) {
isResult = false;
}
}
if (isResult) {
result = "";
for (int n = 0; n < goodQuantity; n++){
result = result + quantityList.get(n) + " ";
}
System.out.println(result);
}
}
quantityList.set(goodQuantity-1, quantityList.get(goodQuantity-1)+1);
}
while (currentSum <= expectedSum);
}
public void tangNghiem(int a) {
quantityList.set(a, quantityList.get(a)+1);
for (int k = a + 1; k < goodQuantity; k++) {
quantityList.set(k, (long) 0);
}
}
public void resetIsChecked(int a) {
for (int k = a + 1; k < goodQuantity; k++) {
isChecked.set(k, false);
}
}
}