알고리즘

[알고리즘] 백준 26711 - A+B (C++)

blueberrysoda 2024. 10. 28. 21:31

[문제 링크] : https://www.acmicpc.net/problem/26711

#include <iostream>
#include <algorithm>
using namespace std;

int N;
string A, B, Ans;
int C;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> A >> B;
    reverse(A.begin(), A.end());
    reverse(B.begin(), B.end());

    while(A.length() < B.length()){
        A += "0";
    }
    while(B.length() < A.length()){
        B += "0";
    }

    int N = A.length();

    for(int i=0; i<N; i++){
        int tmp = A[i] - '0' + B[i] - '0' + C;
        if(tmp > 9){
            C = 1;
            tmp -= 10;
        }
        else{
            C = 0;
        }
        Ans += tmp + '0';
    }

    if(C > 0){
        Ans += '1';
    }

    reverse(Ans.begin(), Ans.end());
    cout << Ans << "\n";
    return 0;
}