如何使用堆栈添加大数?

发布于 2024-12-11 09:10:55 字数 829 浏览 0 评论 0原文

    class LargeNumberPb
{
    Stack s1;
    Stack s2;
    Stack res;
    string first;
    string second;

    public LargeNumberPb()
    {
         first = "10";
         second = "11";
         s1 = new Stack();
         s2 = new Stack();
         res = new Stack();
    }

    public void Fill()
    {
        for (int i = 0; i < first.Length; i++)
        {
            s1.Push(first.Substring(i,1));
        }
        for (int i = 0; i < second.Length; i++)
        {
            s2.Push(second.Substring(i,1));
        }
    }

    public Stack Sum()
    {
        for (int i = 0; i < s.Count; i++)
        {
            res.Push(Convert.ToInt32(s1.Pop()) + Convert.ToInt32(s2.Pop()));
        }
        return res;
    }
}

我想使用堆栈解决添加大量数字的问题。但它没有以正确的方式工作,如果两个数字是 100 和 11,那么进位会去哪里?

    class LargeNumberPb
{
    Stack s1;
    Stack s2;
    Stack res;
    string first;
    string second;

    public LargeNumberPb()
    {
         first = "10";
         second = "11";
         s1 = new Stack();
         s2 = new Stack();
         res = new Stack();
    }

    public void Fill()
    {
        for (int i = 0; i < first.Length; i++)
        {
            s1.Push(first.Substring(i,1));
        }
        for (int i = 0; i < second.Length; i++)
        {
            s2.Push(second.Substring(i,1));
        }
    }

    public Stack Sum()
    {
        for (int i = 0; i < s.Count; i++)
        {
            res.Push(Convert.ToInt32(s1.Pop()) + Convert.ToInt32(s2.Pop()));
        }
        return res;
    }
}

I want to solve adding large number problem, using stack. but it is not working in a right way, if two numbers are 100 and 11, then where the carry will go?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽月 2024-12-18 09:10:55

如果两个数字具有相同的位数,只需修复您的求和函数(仅处理进位,代码未测试):

public Stack Sum()
{
    int carry = 0;
    int count = s1.Count;
    for (int i = 0; i < count; i++)
    {
        var result = Convert.ToInt32(s1.Pop()) + Convert.ToInt32(s2.Pop()) 
                     + carry;
        res.Push(result%10);
        carry = (result - result % 10)/10;
    }

    var carryStream = carry.ToString();
    for(int i=carryStream.Length-1;i>=0;i--)
    {
         res.push(Convert.ToInt32(carryStream.Substring(i,1);
    }
    return res;
}

编辑: 这是不同长度字符串的编辑版本:

        public Stack Sum()
        {
            int carry = 0;
            int count = Math.Min(s1.Count, s2.Count);
            for (int i = 0; i < count; i++)
            {
                var result = Convert.ToInt32(s1.Pop()) + Convert.ToInt32(s2.Pop())
                             + carry;
                res.Push(result % 10);
                carry = (result - result % 10) / 10;
            }

            var carryStream = carry.ToString();
            var remainedStack = s1.Count > 0 ? s1 : s2;
            carry = 0;
            for (int i = carryStream.Length - 1; i >= 0; i--)
            {
                var result = Convert.ToInt32(carryStream.Substring(i, 1)) 
             + (remainedStack.Count > 0 ? Convert.ToInt32(remainedStack.Pop()) : 0);
                res.Push(result % 10);
                carry = (result - result % 10) / 10;
            }

            if (remainedStack.Count > 0)
            {
                int remainedCount = remainedStack.Count;
                for (int i = 0; i < remainedStack.Count; i++)
                {
                    var result = carry + Convert.ToInt32(remainedStack.Pop());
                    res.Push(result % 10);
                    carry = (result - result % 10) / 10;
                }

            }

            if (carry != 0)
                res.Push(carry);

            return res;
        }

If two numbers have equal digit count just fix your sum function (just handle carry, code is not tested):

public Stack Sum()
{
    int carry = 0;
    int count = s1.Count;
    for (int i = 0; i < count; i++)
    {
        var result = Convert.ToInt32(s1.Pop()) + Convert.ToInt32(s2.Pop()) 
                     + carry;
        res.Push(result%10);
        carry = (result - result % 10)/10;
    }

    var carryStream = carry.ToString();
    for(int i=carryStream.Length-1;i>=0;i--)
    {
         res.push(Convert.ToInt32(carryStream.Substring(i,1);
    }
    return res;
}

Edit: and this is a edited version for different length strings:

        public Stack Sum()
        {
            int carry = 0;
            int count = Math.Min(s1.Count, s2.Count);
            for (int i = 0; i < count; i++)
            {
                var result = Convert.ToInt32(s1.Pop()) + Convert.ToInt32(s2.Pop())
                             + carry;
                res.Push(result % 10);
                carry = (result - result % 10) / 10;
            }

            var carryStream = carry.ToString();
            var remainedStack = s1.Count > 0 ? s1 : s2;
            carry = 0;
            for (int i = carryStream.Length - 1; i >= 0; i--)
            {
                var result = Convert.ToInt32(carryStream.Substring(i, 1)) 
             + (remainedStack.Count > 0 ? Convert.ToInt32(remainedStack.Pop()) : 0);
                res.Push(result % 10);
                carry = (result - result % 10) / 10;
            }

            if (remainedStack.Count > 0)
            {
                int remainedCount = remainedStack.Count;
                for (int i = 0; i < remainedStack.Count; i++)
                {
                    var result = carry + Convert.ToInt32(remainedStack.Pop());
                    res.Push(result % 10);
                    carry = (result - result % 10) / 10;
                }

            }

            if (carry != 0)
                res.Push(carry);

            return res;
        }
ぽ尐不点ル 2024-12-18 09:10:55

您只需要一个变量即可继续。当您从 stak 中弹出数字并将其相加时,如果有进位,则将该值分配给您拥有的额外变量。这就是你真正需要的。

public Stack Sum()
{
    int carry = 0 ;
    for (int i = 0; i < s.Count; i++)
    {
        int sum = Convert.ToInt32(s1.Pop()) + Convert.ToInt32(s2.Pop()) + carry;
        if(sum > 10) { carry = 1} else {carry = 0;} // you might want to check more than 10 here
         res.push(sum);
    }
    return res;
}

You need only one variable for carry on. While you are popping numbers from staks and adding them, if there is a carry on, you assign that value to extra variable you have. That s all you need really.

public Stack Sum()
{
    int carry = 0 ;
    for (int i = 0; i < s.Count; i++)
    {
        int sum = Convert.ToInt32(s1.Pop()) + Convert.ToInt32(s2.Pop()) + carry;
        if(sum > 10) { carry = 1} else {carry = 0;} // you might want to check more than 10 here
         res.push(sum);
    }
    return res;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文