웅재의 코딩세상
[프로그래머스] 두 수의 합 본문
- 풀이
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string a, string b) {
string answer ="";
reverse(a.begin(),a.end());
reverse(b.begin(), b.end());
if(a.size() < b.size())swap(a,b);
b.resize(a.size(), '0');
int carry=0;
for(int i=0; i<a.size(); i++){
int sum = (a[i]-'0') + (b[i]-'0') + carry;
carry = sum/10;
sum %= 10;
answer += sum + '0';
}
if(carry) answer += carry+'0';
reverse(answer.begin(), answer.end());
return answer;
}
반응형
'코딩테스트 > 프로그래머스 - LV 0' 카테고리의 다른 글
[프로그래머스] 날짜 비교하기 (0) | 2024.02.01 |
---|---|
[프로그래머스] 이차원 배열 대각선 순회하기 (0) | 2024.02.01 |
[프로그래머스] 문자열 묶기 (0) | 2024.02.01 |
[프로그래머스] 세 개의 구분자 (0) | 2024.02.01 |
[프로그래머스] 빈 배열에 추가, 삭제하기 (0) | 2024.02.01 |