2012-02-27 71 views
4

我必须检查堆栈中的两个值的和等于100,并打印出indecis和数字。我已经使用数组实现了这种可能性,但是我无法使用堆栈使其工作。请帮帮我。直到现在我写下了以下内容,但没有给出正确的结果。Java - 堆栈 - 检查堆栈的两个数字是否相等100

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); 
     } 
    } 
} 
} 
+0

基本堆栈用于编程语言的语法检查(在编译器中)和一些服务策略实现,如LIFO。在你的情况堆栈不是最好的数据结构。 – 2012-02-27 06:27:12

回答

4

作为StackCollection它实现the toArray(T[]) method,所以你可以用它来你的筹码转换成数组,并使用你的工作阵列的解决方案。

但是,您将遇到没有数组自动装箱的问题。自动自动装箱原始类型和对象,这意味着,例如,您可以向Stack直接添加int值,而无需创建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); 
} 

创建数组一次会比多次克隆和排空堆栈更有效。

(虽然如果这是打算教你如何使用堆栈,这可能不是最好的方法!一门功课的问题)

+0

干净,简单和高效 – RAY 2012-02-27 06:15:05

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); 
       } 
      } 
     } 
    } 
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... 
1

您的代码似乎给数字的正确组合,但未能给予指标的正确组合。

这是因为您打电话给pop函数,该函数会从堆栈中移除一个项目,从而将其大小减少1个。因此,您得到的索引是与该时刻的堆栈大小相比的索引。

相反,我会建议,使用peek()get(int index)函数来读取值。我已经更新了get(index)你的榜样,而不克隆堆栈... HV看看...

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); 
       } 
      } 
     } 
    } 
} 
} 
+0

我试过你的方式,但我不知道我只得到3个输出。使用该数组,我获得更多的值作为输出。 – user1234905 2012-02-27 15:19:27

1

这是一种不自然的使用的堆垛(想知道怎么你会在“面向堆栈做到这一点'第四?你会使用一个数组。),所以你只是在努力挣扎。这就是说,只要用你的阵列的解决方案实现这些堆栈运营后:

  1. 深度,这将返回堆栈上的元素的数量。

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

如果你允许使用它们,java.util.Stack中继承了:.size().elementAt()

1

问题与你的筹码基地的解决方案是在这条线。 int value =(Integer)stack.pop(); 一旦你弹出第一个元素,它将从堆栈中消失