-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay51.cpp
67 lines (62 loc) · 1.52 KB
/
Day51.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//Sum of two large numbers
string findSum(string x, string y) {
// Your code goes here
long long i=x.size()-1;
long long j=y.size()-1;
string res="";
long long carry=0;
while(i>=0&&j>=0)
{
stringstream ss1,ss2;
int a,b;
ss1<<x[i];
ss2<<y[j];
ss1>>a;ss2>>b;
long long ans=a+b+carry;
carry=ans/10;
res.append(to_string(ans%10));
i--;
j--;
}
if(i==-1&&j!=-1)
{
while(j>=0)
{
stringstream ss;
int a;
ss<<y[j];
ss>>a;
long long ans=a+carry;
carry=ans/10;
res.append(to_string(ans%10));
j--;
}
}
else if(i!=-1&&j==-1)
{
while(i>=0)
{
stringstream ss;
int a;
ss<<x[i];
ss>>a;
long long ans=a+carry;
carry=ans/10;
res.append(to_string(ans%10));
i--;
}
}
if(carry!=0)
res.append(to_string(carry));
reverse(res.begin(),res.end());
long long k;
for(k=0;k<res.size();k++)
{
if(res[k]!='0')
break;
}
if(k==res.size())
return "0";
res=res.substr(k);
return res;
}