Little Boxes (C++大数加法)

本文深入探讨了一种解决四数之和问题的高效算法,通过使用栈数据结构,该算法能够有效地处理四个整数的求和操作。文章详细解释了算法的工作原理,包括如何从输入的字符串中提取数字并进行求和,同时处理进位问题,确保结果的准确性。此外,还介绍了如何在算法执行过程中避免溢出,并展示了完整的C++代码实现。
摘要由CSDN通过智能技术生成
/***
     *               ii.                                         ;9ABH,          
     *              SA391,                                    .r9GG35&G          
     *              &#ii13Gh;                               i3X31i;:,rB1         
     *              iMs,:,i5895,                         .5G91:,:;:s1:8A         
     *               33::::,,;5G5,                     ,58Si,,:::,sHX;iH1        
     *                Sr.,:;rs13BBX35hh11511h5Shhh5S3GAXS:.,,::,,1AG3i,GG        
     *                .G51S511sr;;iiiishS8G89Shsrrsh59S;.,,,,,..5A85Si,h8        
     *               :SB9s:,............................,,,.,,,SASh53h,1G.       
     *            .r18S;..,,,,,,,,,,,,,,,,,,,,,,,,,,,,,....,,.1H315199,rX,       
     *          ;S89s,..,,,,,,,,,,,,,,,,,,,,,,,....,,.......,,,;r1ShS8,;Xi       
     *        i55s:.........,,,,,,,,,,,,,,,,.,,,......,.....,,....r9&5.:X1
     *       ii55s:.........,,,,,,,,,,,,,,,,.,,,......,.....,,....r9&5.:X1      
     *       59;.....,.     .,,,,,,,,,,,...        .............,..:1;.:&s       
     *      s8,..;53S5S3s.   .,,,,,,,.,..      i15S5h1:.........,,,..,,:99       
     *      93.:39s:rSGB@A;  ..,,,,.....    .SG3hhh9G&BGi..,,,,,,,,,,,,.,83      
     *      G5.G8  9#@@@@@X. .,,,,,,.....  iA9,.S&B###@@Mr...,,,,,,,,..,.;Xh     
     *      Gs.X8 S@@@@@@@B:..,,,,,,,,,,. rA1 ,A@@@@@@@@@H:........,,,,,,.iX:    
     *     ;9. ,8A#@@@@@@#5,.,,,,,,,,,... 9A. 8@@@@@@@@@@M;    ....,,,,,,,,S8    
     *     X3    iS8XAHH8s.,,,,,,,,,,...,..58hH@@@@@@@@@Hs       ...,,,,,,,:Gs   
     *    r8,        ,,,...,,,,,,,,,,.....  ,h8XABMMHX3r.          .,,,,,,,.rX:  
     *   :9, .    .:,..,:;;;::,.,,,,,..          .,,.               ..,,,,,,.59  
     *  .Si      ,:.i8HBMMMMMB&5,....                    .            .,,,,,.sMr
     *  SS       :: h@@@@@@@@@@#; .                     ...  .         ..,,,,iM5
     *  91  .    ;:.,1&@@@@@@MXs.                            .          .,,:,:&S
     *  hS ....  .:;,,,i3MMS1;..,..... .  .     ...                     ..,:,.99
     *  ,8; ..... .,:,..,8Ms:;,,,...                                     .,::.83
     *   s&: ....  .sS553B@@HX3s;,.    .,;13h.                            .:::&1
     *    SXr  .  ...;s3G99XA&X88Shss11155hi.                             ,;:h&,
     *     iH8:  . ..   ,;iiii;,::,,,,,.                                 .;irHA  
     *      ,8X5;   .     .......                                       ,;iihS8Gi
     *         1831,                                                 .,;irrrrrs&@
     *           ;5A8r.                                            .:;iiiiirrss1H
     *             :X@H3s.......                                .,:;iii;iiiiirsrh
     *              r#h:;,...,,.. .,,:;;;;;:::,...              .:;;;;;;iiiirrss1
     *             ,M8 ..,....,.....,,::::::,,...         .     .,;;;iiiiiirss11h
     *             8B;.,,,,,,,.,.....          .           ..   .:;;;;iirrsss111h
     *            i@5,:::,,,,,,,,.... .                   . .:::;;;;;irrrss111111
     *            9Bi,:,,,,......                        ..r91;;;;;iirrsss1ss1111
     */

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
const int maxn = 1010;

void asd(int x[], char y[])
{
    int len = strlen(y);
    for (int i = 0; i < len; i++)
    {
        x[i] = y[i] - '0';
    }
}

stack<int> sa, sb, sc, sd, ans;

int main()
{
    ll T;
    int a[100], b[100], c[100], d[100];
    char aa[100], bb[100], cc[100], dd[100];
    int la, lb, lc, ld;
    cin >> T;
    while (T--)
    {
        scanf("%s %s %s %s", aa, bb, cc, dd);
        la = strlen(aa);
        lb = strlen(bb);
        lc = strlen(cc);
        ld = strlen(dd);

        asd(a, aa);
        asd(b, bb);
        asd(c, cc);
        asd(d, dd);

        for (int i = 0; i < la; i++)
            sa.push(a[i]);
        for (int i = 0; i < lb; i++)
            sb.push(b[i]);
        for (int i = 0; i < lc; i++)
            sc.push(c[i]);
        for (int i = 0; i < ld; i++)
            sd.push(d[i]);

        int pir = 0;

        for (;;)
        {
            if (sa.empty() && sb.empty() && sc.empty() && sd.empty())
                break;
            int aaa = 0, bbb = 0, ccc = 0, ddd = 0;
            if (!sa.empty())
            {
                aaa = sa.top();
                sa.pop();
            }
            if (!sb.empty())
            {
                bbb = sb.top();
                sb.pop();
            }
            if (!sc.empty())
            {
                ccc = sc.top();
                sc.pop();
            }
            if (!sd.empty())
            {
                ddd = sd.top();
                sd.pop();
            }
            int sum = aaa + bbb + ccc + ddd;
            ans.push((sum+pir)%10);
            pir = (sum+pir) / 10;
        }
        if(pir!=0)
            ans.push(pir);
        while (!ans.empty())
        {
            cout << ans.top();
            ans.pop();
        }
        cout << endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值