BOJ 백준 15353 큰 수 A+B(2) 풀이
사용언어
Visual studio 2019 C++
[15353 풀이]
#include <iostream>
#include <string>
#include <algorithm>
#define endl "\n"
using namespace std;
int pl = 0;
string x, y;
string result = "";
int main() {
cin >> x >> y;
reverse(x.begin(), x.end());
reverse(y.begin(), y.end());
// 숫자길이가 맞지않을경우
while (x.length() < y.length()) {
x += '0';
}
while (x.length() > y.length()) {
y += '0';
}
for (int i = 0; i < x.length(); ++i) {
int num = (x[i] - '0' + y[i] - '0' + pl) % 10;
result += to_string(num);
pl = (x[i] - '0' + y[i] - '0' + pl) / 10;
}
if (pl != 0) {
result += to_string(pl);
}
reverse(result.begin(), result.end());
cout << result << endl;;
}
Comments