You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现每个单元含两个数的栈?Java函数编写求助

问题描述

给定如下TwoNumbers类:

public class TwoNumbers{ private int num1, num2; public TwoNumbers (int num1, int num2){ this.num1 = num1; this.num2 = num2; } }

需要实现函数public Stack<TwoNumbers> func(Stack<Integer> st);,要求输入栈st为[8,4,7,5,3,2]时,输出栈st_final为[num1=5 | num2=7 , num1=3 | num2=4 , num1=2 | num2=8 ]。

已完成部分代码:

public static void main(String[] args) {
    Stack<Integer> st = new Stack<Integer>();
    st.push(8);
    st.push(4);
    st.push(7);
    st.push(5);
    st.push(3);
    st.push(2);
    func(st);

}
public static Stack<TwoNumbers> func(Stack<Integer> st){
    Stack<Integer> st_top = new Stack<Integer>();
    Stack<TwoNumbers> st_final = new Stack<TwoNumbers>();
    int i;
    System.out.println("input st:"+st);

    for(i=0;i<=st.size()/2;i++) 
        st_top.push(st.pop());
        
    
    System.out.println("st_top:"+st_top);
    System.out.println("st_bottom"+st);
   
    return st_final;
}

不清楚如何将值存入st_final栈,请求技术帮助。

解决方案

首先纠正你现有代码的循环问题:原循环条件i<=st.size()/2会因为栈size()动态变化导致多弹出元素,应该先固定总元素数和需要拆分的数量,避免逻辑错误。

结合输入输出的对应关系,正确的实现步骤如下:

  1. 先获取原栈的总元素数,计算出需要拆分的一半数量(这里总共有6个元素,拆分为各3个的两个栈)
  2. 将原栈顶的3个元素弹出到st_top,此时st中剩余原栈的下半部分[8,4,7],st_top为[2,3,5]
  3. 循环从两个栈中分别弹出元素,按要求创建TwoNumbers对象并压入结果栈st_final

修改后的完整代码如下:

public class TwoNumbers {
    private int num1, num2;
    public TwoNumbers(int num1, int num2) {
        this.num1 = num1;
        this.num2 = num2;
    }
    // 重写toString方法,保证输出符合要求格式
    @Override
    public String toString() {
        return "num1=" + num1 + " | num2=" + num2;
    }
}

public class StackProcessor {
    public static void main(String[] args) {
        Stack<Integer> st = new Stack<Integer>();
        st.push(8);
        st.push(4);
        st.push(7);
        st.push(5);
        st.push(3);
        st.push(2);
        Stack<TwoNumbers> result = func(st);
        System.out.println("output st_final:" + result);
    }

    public static Stack<TwoNumbers> func(Stack<Integer> st) {
        Stack<Integer> st_top = new Stack<Integer>();
        Stack<TwoNumbers> st_final = new Stack<TwoNumbers>();
        int totalSize = st.size();
        int halfSize = totalSize / 2;

        System.out.println("input st:" + st);

        // 弹出栈顶的halfSize个元素到st_top
        for (int i = 0; i < halfSize; i++) {
            st_top.push(st.pop());
        }

        System.out.println("st_top:" + st_top);
        System.out.println("st_bottom:" + st);

        // 配对元素并压入st_final
        while (!st_top.isEmpty() && !st.isEmpty()) {
            int num1 = st_top.pop();
            int num2 = st.pop();
            st_final.push(new TwoNumbers(num1, num2));
        }

        return st_final;
    }
}

关键说明:

  • 提前固定totalSize和halfSize,避免循环中栈长度变化引发的错误
  • 重写TwoNumbers的toString()方法,确保打印结果栈时能显示指定格式的内容
  • 配对时从两个栈分别弹出元素,按要求的顺序创建TwoNumbers实例并压入结果栈

运行后输出结果即为需求中的[num1=5 | num2=7 , num1=3 | num2=4 , num1=2 | num2=8 ]

内容的提问来源于stack exchange,提问作者CodingHell2312x

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 22:56:29