a+b

#include<bits/stdc++.h>

using namespace std;
typedef vector<int> VI;

vector<int> add(vector<int> &A, vector<int> &B)  // C = A + B, A >= 0, B >= 0
{
    if (A.size() < B.size()) return add(B, A);

    vector<int> C;
    int t = 0;
    for (int i = 0; i < A.size(); i ++ )
    {
        t += A[i];
        if (i < B.size()) t += B[i];
        C.push_back(t % 10);
        t /= 10;
    }

    if (t) C.push_back(t);
    return C;
}


int main() {
    //ios::sync_with_stdio(false);
    //freopen("D://out.txt","w",stdout);
    int T;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        VI A, B;
        string a, b;
        cin >> a >> b;
        int lsa, lsb;
        for (int i = 0; i <= a.size() - 1; i++) {
            if (a[i] != 0) {
                lsa = i;
                break;
            }
        }
        for (int i = 0; i <= b.size() - 1; i++) {
            if (b[i] != 0) {
                lsb = i;
                break;
            }
        }
        cout << lsa << lsb << endl;
        for (int i = a.size() - 1; i >= lsa; i--) A.push_back(a[i] - 0);
        for (int i = b.size() - 1; i >= lsb; i--) B.push_back(b[i] - 0);
        VI C = add(A, B);
        cout << "Case " << t << ":" << endl;
        cout << a << " + " << b << " = ";
        for (int i = a.size() - 1; i >= 0; i--) cout << C[i];
        cout << endl;
        cout << endl;
    }

    return 0;
}

 

a+b

上一篇:noip模拟44[我想我以后会碰见计数题就溜走的]


下一篇:Vue通过导航栏实现路由跳转