티스토리 뷰

백준

백준 소스코드 [C++] 10757 큰 수 A+B

Hani_Levenshtein 2020. 10. 29. 20:54

www.acmicpc.net/problem/10757

 

10757번: 큰 수 A+B

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.

www.acmicpc.net

백준 소스코드 [C++] 10757 큰 수 A+B

#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
string bigadd(string a, string b) {
	string c;
	int as, bs;
	int bit = 0;
	while (!a.empty() && !b.empty()) {
		as = a.back() - '0'; bs = b.back() - '0';
		c = (char)(((as + bs + bit) % 10) + '0') + c;
		bit = (as + bs + bit) / 10;
		a.pop_back();b.pop_back();
	}
	while (!a.empty()) {
		as = a.back() - '0';
		c = (char)(((as + bit) % 10) + '0') + c;
		bit = (as + bit) / 10;
		a.pop_back();
	}
	while (!b.empty()) {
		bs = b.back() - '0';
		c = (char)(((bs + bit) % 10) + '0') + c;
		bit = (bs + bit) / 10;
		b.pop_back();
	}
	if (bit == 1)c = (char)(bit + '0') + c;
	return c;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string a, b;
    cin >> a >> b;
    cout<<bigadd(a,b);
    return 0;
}
댓글