File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+ using namespace std ;
3+ int main () {
4+ ios_base::sync_with_stdio (false );
5+ cin.tie (NULL );
6+ cout.tie (NULL );
7+ string A, B, ret;
8+ cin >> A >> B;
9+ int carr = 0 ;
10+ bool A_large;
11+ int cnt, aidx = A.size ()-1 , bidx = B.size ()-1 ;
12+ if (A.size () > B.size ()) {
13+ A_large = true ;
14+ cnt = B.size ();
15+ } else {
16+ A_large = false ;
17+ cnt = A.size ();
18+ }
19+
20+ while (cnt--) {
21+ int a, b;
22+ a = A[aidx--] - ' 0' ;
23+ b = B[bidx--] - ' 0' ;
24+ if (a+b+carr >= 10 ) {
25+ ret = to_string ((a + b + carr - 10 )) + ret;
26+ carr = 1 ;
27+ } else {
28+ ret = to_string (a + b + carr) + ret;
29+ carr = 0 ;
30+ }
31+ }
32+
33+ if (A_large) {
34+ while (true ){
35+ if (aidx == -1 ) break ;
36+ int a = A[aidx--] - ' 0' ;
37+ if (a+carr >= 10 ) {
38+ ret = to_string ((a + carr - 10 )) + ret;
39+ carr = 1 ;
40+ } else {
41+ ret = to_string (a + carr) + ret;
42+ carr = 0 ;
43+ }
44+ }
45+ } else {
46+ while (true ){
47+ if (bidx == -1 ) break ;
48+ int b = B[bidx--] - ' 0' ;
49+ if (b+carr >= 10 ) {
50+ ret = to_string ((b + carr - 10 )) + ret;
51+ carr = 1 ;
52+ } else {
53+ ret = to_string (b + carr) + ret;
54+ carr = 0 ;
55+ }
56+ }
57+ }
58+ if (carr) {
59+ ret = to_string (carr) + ret;
60+ }
61+
62+ cout << ret << ' \n ' ;
63+ return 0 ;
64+ }
You can’t perform that action at this time.
0 commit comments