less than 1 minute read

사용언어

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;;
}

Categories:

Updated:

Comments