알고리즘

[알고리즘] 백준 5893 - 17배 (C++)

blueberrysoda 2024. 12. 20. 23:32

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

#include <iostream>
#include <algorithm>
using namespace std;
 
int main(){
    string A, B, Ans;
    cin >> A;
    B = A;
    A += "0000";
    int tmp = 0;

    while(B.empty() == false){
        int val = A.back() + B.back() - 96 + tmp;
        tmp = val / 2;
        Ans += to_string(val % 2);
        A.pop_back();
        B.pop_back();
    }

    while(A.empty() == false){
        int val = A.back() - 48 + tmp;
        tmp = val / 2;
        Ans += to_string(val % 2);
        A.pop_back();
    }

    if(tmp){
        Ans += "1";
    }
    reverse(Ans.begin(), Ans.end());
    cout << Ans << "\n";
    return 0;
}