-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dodawanie duzych liczb.cpp
53 lines (41 loc) · 1.05 KB
/
Dodawanie duzych liczb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
/// adding two big integers
std::string add(std::string s1, std::string s2);
int main() {
std::ios::sync_with_stdio(false);
int t;
std::cin >> t; /// number of tests
std::string s1, s2;
while(t--){
std::cin >> s1 >> s2;
std::cout << add(s1,s2) << std::endl;
}
return 0;
}
std::string add(std::string s1, std::string s2)
{
int i = s1.length();
int j = s2.length();
int min = i;
if(j < i) min = j; /// length of shorter of two strings
int p = 0;
std::string result = "";
int val = 0;
for(int it = 1; it <= min; it++){
val = (int)(s1[--i]) + (int)(s2[--j]) + p - 96;
p = val / 10;
result = (char)((val % 10) + 48) + result;
}
while(i){
val = s1[--i] + p - 48;
p = val / 10;
result = (char)((val % 10) + 48) + result;
}
while(j){
val = s2[--j] + p - 48;
p = val / 10;
result = (char)((val % 10) + 48) + result;
}
if(p) result = (char)(p + 48) + result;
return result;
}