删除整数栈中的前半部分元素。

huangapple go评论72阅读模式
英文:

Delete the first half elements from stack of integer

问题

import java.util.*;

public class DeleteElement {
    public static void main(String args[]) {
        Stack<Integer> stack = new Stack<>();
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        while (n-- > 0)
            stack.push(s.nextInt());
        deleteFirstHalf(stack, stack.size(), 0);
        System.out.println(stack);
    }

    static void deleteFirstHalf(Stack<Integer> stack, int n, int curr) {
        if(stack.empty() || curr == n) {
            return;
        }
        int x = stack.pop();

        deleteFirstHalf(stack, n, curr+1);

        if(curr < Math.floor(n/2)){
            stack.push(x);
        }
    }
}
英文:

You are given a stack with n integers. You need to delete floor(n/2) elements from the bottom of the stack and print the remaining elements of the stack. The remaining elements should be printed in the order that they are inserted into the stack.

floor(3.5) will give the output as 3, greatest integer less than or equal to the input.
Input Format:
The first line of input is an integer n denoting the size of stack. The next line contains n space separated integers.
Output Format:
The remaining elements of the stack after removal of the required elements.
Example:
Stack(bottom -> top) = [1, 2, 3, 4, 5, 6]
Output: [4, 5, 6]

Stack = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Output: [6, 7, 8, 9, 10, 11]

Sample Input:
12
1 2 3 4 5 6 7 8 9 10 11 12

Sample Output:
[7, 8, 9, 10, 11, 12]

Sample input:
19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Sample Output:
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Here my problem is my code passed test case 1 and test case 2 but in test case 3 where sample input is 19 there is should be printing as mentioned above output but my output came [11, 12, 13, 14, 15, 16, 17, 18, 19] its omitting 10th element and i dont know where is my error. So please help me in this

MY Code -

import java.util.*;

public class DeleteElement {
    public static void main(String args[]) {
        Stack&lt;Integer&gt; stack = new Stack&lt;&gt;();
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        while (n-- &gt; 0)
            stack.push(s.nextInt());
        deleteFirstHalf(stack, stack.size(), 0);
        System.out.println(stack);
    }

    static void deleteFirstHalf(Stack&lt;Integer&gt; stack, int n, int curr) {


        if(stack.empty() || curr == n) {
            return;
        }
        int x = stack.pop();

        deleteFirstHalf(stack, n, curr+1);

        if(curr&lt;Math.floor(n/2)){
            stack.push(x);
        }
    }
}

Note - Don't change the main() in my code.

答案1

得分: 1

我实际上尝试了一种不同的方法,其中我创建了两个不同的堆栈 temptemp1,而不是递归调用函数,我进行了内部检查并在最后打印了输出。

static void deleteFirstHalf(Stack<Integer> stack) {
    Stack<Integer> temp = new Stack<Integer>();
    Stack<Integer> temp1 = new Stack<Integer>();
    int i = stack.size();
    int dest = stack.size() / 2;
    while (i-- > dest) {
        int t = stack.pop();
        temp.push(t);
    }
    while (!temp.empty()) {
        int t = temp.pop();
        // System.out.print(i + " size");
        temp1.push(t);
    }
    System.out.print(temp1);
}
英文:

I actually tried that with a Different Approach, where I created 2 different stacks temp and temp1, instead of recursively calling the function I had internal checks and printed the output at the end

static void deleteFirstHalf(Stack&lt;Integer&gt; stack) {
    Stack&lt;Integer&gt; temp = new Stack&lt;Integer&gt;();
    Stack&lt;Integer&gt; temp1 = new Stack&lt;Integer&gt;();
    int i = stack.size();
    int dest = stack.size() / 2;
    while (i-- &gt; dest) {
        int t = stack.pop();
        temp.push(t);
      }
    while (!temp.empty()) {
        int t = temp.pop();
        // System.out.print(i + &quot; size&quot;);
        temp1.push(t);
    }
    System.out.print(temp1);
}

答案2

得分: 0

你的错误源于你应该删除底部的 floor(n/2) 个元素,但实际上你保留了顶部的 floor(n/2) 个元素。它能够在某些测试案例中工作的原因是,当 n 为偶数时,保留顶部的 n/2 个元素与删除底部的 n/2 个元素是相同的。当 n 为奇数时,代码应该保留一个额外的元素。

解决方案是将 if (curr &lt; Math.floor(n/2)) 更改为 if (curr &lt; n/2 + n%2)。首先,你可以去掉 Math.floor(),因为整数除法本身就执行向下取整。关键的区别在于添加了 n%2。当 n 为偶数时,n%2 的值为 0,当 n 为奇数时,n%2 的值为 1。这样做的效果是,如果 n 为奇数,代码将保留一个额外的元素(在第3个测试案例中,它将保留 10 而不是删除它)。如果 n 为偶数,n%2 的值为 0,因此算法的行为与以前相同。

英文:

Your bug comes from the fact that you're supposed to delete the bottom floor(n/2) elements, but instead you're keeping the top floor(n/2) elements. The reason it works for some test cases is that when n is even, keeping the top n/2 elements is the same as deleting the bottom n/2 elements. When n is odd, the code should keep one extra element.

The solution is to change if (curr &lt; Math.floor(n/2)) to if (curr &lt; n/2 + n%2). For one, you can drop the Math.floor(), since integer division does floor division anyway. The key difference is the addition of n%2. n%2 evaluates to 0 when n is even, and 1 when n is odd. What this does is if n is odd, it will make the code keep one extra element. (in the case of the 3rd test case, it will keep 10 instead of deleting it) If n is even, n%2 evaluates to 0, so the algorithm behaves the same as before.

答案3

得分: 0

替代 curr<Math.floor(n/2) 尝试使用 n-curr > Math.floor(n/2)

英文:

Instead of curr<Math.floor(n/2) try n-curr > Math.floor(n/2)

答案4

得分: 0

替代 (curr < Math.floor(n/2)) 尝试使用 (curr < n - Math.floor(n/2))

英文:

Instead of (curr<Math.floor(n/2)) try (curr < n-Math.floor(n/2))

huangapple
  • 本文由 发表于 2020年9月18日 15:04:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63950803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定