两个大数相加比较符合自然运算的算法

功能很简单,就是实现两个很长的大数相加的功能
 1 None.gif public   static   string  MaxNumAdd( string  num1,  string  num2)
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 3InBlock.gif            //初始设置进位为0
 4InBlock.gif            int carry = 0;
 5InBlock.gif            int length1 = num1.Length;
 6InBlock.gif            int length2 = num2.Length;
 7InBlock.gif            Stack<int> stack1 = new Stack<int>(length1);
 8InBlock.gif            Stack<int> stack2 = new Stack<int>(length2);
 9InBlock.gif            int max = length1;
10InBlock.gif            if (max < length2)
11ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
12InBlock.gif                max = length2;
13ExpandedSubBlockEnd.gif            }

14InBlock.gif            //用于存放结果
15InBlock.gif            Stack<int> stack3 = new Stack<int>(max + 1);
16InBlock.gif            int size = max - length1;
17InBlock.gif            while (size > 0)
18ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
19InBlock.gif                stack1.Push(0);
20InBlock.gif                size--;
21ExpandedSubBlockEnd.gif            }

22InBlock.gif            size = max - length2;
23InBlock.gif            while (size > 0)
24ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
25InBlock.gif                stack2.Push(0);
26InBlock.gif                size--;
27ExpandedSubBlockEnd.gif            }

28InBlock.gif            for (int i = 0; i < length1; i++)
29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
30InBlock.gif               
31InBlock.gif                int num = Convert.ToInt32(num1[i].ToString());
32InBlock.gif                stack1.Push(num);
33ExpandedSubBlockEnd.gif            }

34InBlock.gif            for (int i = 0; i < length2; i++)
35ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
36InBlock.gif               
37InBlock.gif                int num = Convert.ToInt32(num2[i].ToString());      
38InBlock.gif                stack2.Push(num);
39ExpandedSubBlockEnd.gif            }

40InBlock.gif            //临时施计算结果
41InBlock.gif            int tempResult = 0;
42InBlock.gif            for (int i = 0; i < max; i++)
43ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
44InBlock.gif                int n1 = stack1.Pop();
45InBlock.gif                int n2 = stack2.Pop();
46InBlock.gif                tempResult = n1 + n2 + carry;
47InBlock.gif                stack3.Push(tempResult);
48InBlock.gif                if (tempResult > 9)
49ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
50InBlock.gif                    carry = 1;
51ExpandedSubBlockEnd.gif                }

52InBlock.gif                else
53ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
54InBlock.gif                    carry = 0;
55ExpandedSubBlockEnd.gif                }

56ExpandedSubBlockEnd.gif            }

57InBlock.gif            stack3.Push(carry);
58InBlock.gif            StringBuilder sb = new StringBuilder();
59InBlock.gif            while (stack3.Count > 0)
60ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
61InBlock.gif                sb.Append(stack3.Pop().ToString());
62ExpandedSubBlockEnd.gif            }

63InBlock.gif            return sb.ToString().TrimStart('0');
64ExpandedBlockEnd.gif        }

65 None.gif    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值