[알고리즘] 뒤집힌 덧셈 - 백준 1357

se.jeon·2023년 3월 17일
0

알고리즘

목록 보기
19/21

문제

과정

간단한 브론즈 1 문제인데, 너무 복잡하게 풀었나 싶기도...

숫자를 문자로 취급하고 자릿수에 따라 제곱연산을 해 주면서 스택을 이용해서 단어뒤집기 때와 비슷하게 풀었다.

마지막 자리가 0으로 끝나지 않게 제거 해 준다.

결과!

//
// Created by 전시은 on 2023/03/11.
//

// 문제 :: 뒤집힌 덧셈
// 링크 :: https://www.acmicpc.net/problem/2309
// 입력 :: 첫째 줄에 수 X와 Y가 주어진다. X와 Y는 1,000보다 작거나 같은 자연수이다.
// 출력 :: 째 줄에 문제의 정답을 출력한다.

#include "../Problems.h"
#include <iostream>
#include <string>
#include <stack>
#include <cmath>
#include <algorithm>
using namespace std;


//int main()
int Solve1357()
{
    cout << "[디버깅용] Solve1357 :: 시작지점 >> \n";

    cin.tie(NULL);
    ios_base::sync_with_stdio(false);

    string sInput;
    stack<char> cInput;
    int nSum = 0;

    for(int i = 0; i < 2; i++)
    {
        cin >> sInput;

        for(int j = 0; j < sInput.size(); j++)
        {
            cInput.push(sInput.at(j));
        }
        for(int j = 0; j < sInput.size(); j++)
        {
            nSum += (cInput.top() - '0') * pow(10, sInput.size() - j - 1);
            cInput.pop();
        }
    }

    sInput = to_string(nSum);
    while(sInput.at(sInput.size()-1) == '0')
    {
        sInput.pop_back();
    }

    reverse(sInput.begin(), sInput.end());

    cout << sInput;

    return 0;
}
profile
취미 다이소

0개의 댓글