牛客小白月赛 22 J.计算A+B

**

因为热爱 所以坚持

**

题目连接

https://ac.nowcoder.com/acm/contest/4462/J

纯模拟,第一次自己敲高精还只是高竞加居然敲了这么长时间,万幸1A!

#include <iostream>
#include <malloc.h>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#define IO                       \
    ios::sync_with_stdio(false); \
    // cin.tie(0);                  \
    // cout.tie(0);
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
int c[maxn];
int a[maxn];
int b[maxn];
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    IO;
    int T;
    string s;
    cin >> T;
    while (T--)
    {
        cin >> s;
        int n = s.size();
        memset(c, 0, sizeof c);
        memset(a, 0, sizeof a);
        memset(b, 0, sizeof b);
        int p = -1;
        for (int i = 0; i < n; i++)
            if (s[i] == '+')
            {
                p = i;
                break;
            }
        if (p == -1 || p == n - 1 || p == 0)
        {
            cout << "skipped" << endl;
            continue;
        }
        int lena = p;
        int lenb = n - p - 1;
        for (int i = p - 1; i >= 0; i--)
        {
            a[p - i] = int(s[i] - '0');
        }
        for (int i = n - 1; i > p; i--)
        {
            b[n - i] = int(s[i] - '0');
        }
        for (int i = 1; i <= max(lena, lenb); i++)
        {
            c[i] += (a[i] + b[i]);
        }
        for (int i = 1; i <= max(lena, lenb); i++)
        {
            int t = c[i] / 10;
            c[i + 1] += t;
            c[i] %= 10;
        }
        int pos = -1;
        for (int i = 3000; i >= 1; i--)
        {
            if (c[i] != 0)
            {
                pos = i;
                break;
            }
        }
        if (pos == -1)
            cout << 0 << endl;
        else
        {
            for (int i = pos; i >= 1; i--)
                cout << c[i];
            cout << endl;
        }
    }
    return 0;
}
发布了81 篇原创文章 · 获赞 29 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44115065/article/details/104453097