문제 링크입니다: https://www.acmicpc.net/problem/3486
3486번: Adding Reversed Numbers
The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore the dramatic advisor of ACM has decided to transfigure some tragedies into comedies. Obviously, this work is very hard beca
www.acmicpc.net
간단한 문자열 처리 문제였습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <algorithm> | |
using namespace std; | |
string getResultWithoutUnnecessaryZeros(string result) | |
{ | |
// 최초로 0이 아닌 인덱스를 찾는다 | |
int firstNonZeroIdx = result.size(); | |
for (int i = 0; i < result.size(); i++) | |
{ | |
if (result[i] != '0') | |
{ | |
firstNonZeroIdx = i; | |
break; | |
} | |
} // 모든 자리가 0이라면 0을 반환 | |
if (firstNonZeroIdx == result.size()) | |
{ | |
return "0"; | |
} | |
// 최초로 0이 아닌 자리부터의 숫자들을 반환 | |
return result.substr(firstNonZeroIdx); | |
} | |
void printReverseSum(string a, string b) | |
{ | |
reverse(a.begin(), a.end()); | |
reverse(b.begin(), b.end()); | |
string sum = to_string(stoi(a) + stoi(b)); | |
reverse(sum.begin(), sum.end()); | |
cout << getResultWithoutUnnecessaryZeros(sum) << "\n"; | |
} | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
int N; | |
cin >> N; | |
for (int n = 0; n < N; n++) | |
{ | |
string a, b; | |
cin >> a >> b; | |
printReverseSum(a, b); | |
} | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 3533번 Explicit Formula (0) | 2021.05.15 |
---|---|
백준 3507번 Automated Telephone Exchange (0) | 2021.05.15 |
백준 3460번 이진수 (0) | 2021.05.14 |
백준 3009번 네 번째 점 (0) | 2021.05.14 |
백준 3028번 창영마을 (0) | 2021.05.14 |