문제 : https://www.acmicpc.net/problem/10824


[ 알고리즘풀이 ]

C++ STL String을 이용하면 입력받은 4개의 수 중 두개씩 Concat 할 수 있고, 그 후 문자열을 활용한 덧셈을 진행하면 된다.

(문자열을 활용한 덧셈 포스팅 : https://travelbeeee.tistory.com/193?category=800452 )

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

string stringAdd(string A, string B) {
	string temp = {};
	int sum = 0;
	while (!A.empty() || !B.empty() || sum) {
		if (!A.empty()) {
			sum += A.back() - '0';
			A.pop_back();
		}
		if (!B.empty()) {
			sum += B.back() - '0';
			B.pop_back();
		}
		temp.push_back((sum % 10) + '0');
		sum /= 10;
	}
	reverse(temp.begin(), temp.end());
	return temp;
}

int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);

	string a, b, c, d;
	cin >> a >> b >> c >> d;
	cout << stringAdd(a + b, c + d);
}

 

+ Recent posts