Java - 堆栈 - 检查堆栈的2个数字是否等于100

时间:2012-02-27 06:00:03

标签: java arrays stack

我必须检查堆栈中哪两个值等于100的总和并打印出indecis和数字。我已经使用数组使这成为可能但我无法使用堆栈工作。请帮我。我写了以下内容直到现在,并没有给我正确的输出。

import java.util.Stack;

public class 2 {

public static void main(String[] args) {

    int x = 100;
    Stack stack=new Stack();
    Stack tempStack=new Stack();
    stack.push(new Integer(20));
    stack.push(new Integer(53));
    stack.push(new Integer(41));
    stack.push(new Integer(38));
    stack.push(new Integer(28));
    stack.push(new Integer(47));
    stack.push(new Integer(70));
    stack.push(new Integer(30));
    stack.push(new Integer(80));
    stack.push(new Integer(400));
    stack.push(new Integer(3));
    stack.push(new Integer(20));

    tempStack = (Stack) stack.clone();
    for (int i=0; i<stack.size(); i++) {
        tempStack = (Stack) stack.clone();
        int value = (Integer) stack.pop();
        if (!stack.isEmpty()) {
            for (int k=0; k<tempStack.size(); k++) {
                int tmp = (Integer) tempStack.pop();
                if ((value + tmp) == x) {
                    System.out.println("Indices " + i + " & " + k + " with values " 
                            + value + " & " + tmp);
                }
            }
        }
    }
}
}

以下是基于阵列的解决方案:

public class 1 {

public static void main(String[] args) {

    int x = 100;
    int [] array = {20,3,400,80,30,70,20,47,28,38,41,53,20};
    for (int i=0; i<array.length; i++){
        int temp1 = array[i];
        for (int k=1; k<array.length; k++) {
            int temp2 = array[k];
            if ((temp1+temp2)==x)
                System.out.println("Indices " + i + " & " + k + " with values " 
                        + temp1 + " & " + temp2);
        }
    }
}
}

6 个答案:

答案 0 :(得分:4)

由于StackCollection,它实现了the toArray(T[]) method,因此您可以使用它将堆栈转换为数组并使用工作数组解决方案。

但是,您将遇到阵列没有自动装箱的问题。自动装箱会自动在原始类型和对象之间进行转换,这意味着,例如,您可以直接向int添加Stack值,而无需创建Integer个对象,因为编译器会为您执行此操作:

Stack<Integer> stack = new Stack<Integer>();
stack.push(20);
stack.push(53);

但是,编译器不会在int[]Integer[]之间进行转换,因此您必须这样做:

Integer[] array = stack.toArray(new Integer[stack.size()]);

使用Integer[]将是一件苦差事。

所以最简单的事情就是:

int[] array = new int[stack.size()];

for (int i = 0; i < array.length; i++) {
  array[i] = stack.get(i);
}

创建一次数组比重复克隆和清空Stack更有效。

(虽然如果这是一个家庭作业问题,旨在教你如何使用堆栈,这可能不是最好的方法!)

答案 1 :(得分:1)

巡回逻辑几乎没有变化,不要在循环中取stack.size(),在每次循环迭代时递减,所以你只迭代半循环

int stackSize = stack.size();
    for (int i=0; i<stackSize; i++) {
        tempStack = (Stack) stack.clone();
        int value = (Integer) stack.pop();
        if (!stack.isEmpty()) {
            int tempSize = tempStack.size();
            for (int k=0; k<tempSize; k++) {
                int tmp = (Integer) tempStack.pop();
                if ((value + tmp) == x) {
                    System.out.println("Indices " + i + " & " + k + " with values " 
                            + value + " & " + tmp);
                }
            }
        }
    }

答案 2 :(得分:1)

第二个堆栈的索引可能不正确,因为当您在循环内克隆初始堆栈时,每次迭代它都会更小。

stack = {25,50}
stack.clone => {25,50}
stack.pop => 25
stack.clone => {50}
thus, if 50+50== 100 the indicies found would be i=1, k=0 instead of 1,1...

答案 3 :(得分:1)

您的代码似乎给出了正确的Numbers组合,但它无法提供正确的Index组合。

这是因为您正在调用pop函数,该函数从堆栈中删除项目,从而将其大小减小1.因此,您获得的索引是与该时刻的堆栈大小相比的索引。

相反,我建议使用peek()get(int index)函数来读取值。我已经使用get(index)更新了您的示例,而没有克隆堆栈...看看......

import java.util.Stack;

public class Class2 {

public static void main(String[] args) {

    int x = 100;
    Stack stack=new Stack();
    Stack tempStack=new Stack();
    stack.push(new Integer(20));
    stack.push(new Integer(53));
    stack.push(new Integer(41));
    stack.push(new Integer(38));
    stack.push(new Integer(28));
    stack.push(new Integer(47));
    stack.push(new Integer(70));
    stack.push(new Integer(30));
    stack.push(new Integer(80));
    stack.push(new Integer(400));
    stack.push(new Integer(3));
    stack.push(new Integer(20));

   // tempStack = (Stack) stack.clone();
    for (int i=0; i<stack.size(); i++) {
       // tempStack = (Stack) stack.clone();
        int value = (Integer) stack.get(i);
        if (!stack.isEmpty()) {
            for (int k=i+1; k<stack.size()-1; k++) {
                int tmp = (Integer) stack.get(k);
                System.out.println("Value"+value+" tmp "+tmp+"Stack size"+stack.size());
                if ((value + tmp) == x) {
                    System.out.println("Indices " + i + " & " + k + " with values " 
                            + value + " & " + tmp);
                }
            }
        }
    }
}
}

答案 4 :(得分:1)

这是一个不自然的堆栈使用(想要知道你如何在'面向堆栈的'Forth中做到这一点?你会使用一个数组。),所以你正在努力奋斗。也就是说,在实现这些堆栈运算符后,只需使用您的数组解决方案:

  1. DEPTH,返回堆栈中元素的数量。

  2. PICK,它将索引处的元素返回到堆栈中。

  3. 如果您被允许使用它们,java.util.Stack将继承:.size().elementAt()

答案 5 :(得分:1)

您的堆栈基础解决方案的问题就在这一行。 int value =(Integer)stack.pop();  一旦弹出第一个元素,它将从堆栈中消失

相关问题