-
Notifications
You must be signed in to change notification settings - Fork 1
/
10013.cpp
55 lines (51 loc) · 936 Bytes
/
10013.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
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string a, b, c;
char s1[1000010], s2[1000010];
string add(string a,string b)
{
string c;
int i,j,k,carry,len1=a.length(),len2=b.length(),maxlen=len2>len1 ? len2 : len1;
for(carry=i=0;i<maxlen || carry;i++)
{
j= i<len1 ? a[len1-i-1]-'0':0;
k= i<len2 ? b[len2-i-1]-'0':0;
c+=((j+k+carry)%10)+'0';
carry=(j+k+carry)/10;
}
reverse(c.begin(),c.end());
return c;
}
int main()
{
freopen("10013.txt", "r", stdin);
int t, i, n, len, n1, n2;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(i=0; i<n; i++)
{
scanf("%d%d", &n1, &n2);
s1[i] = n1+48;
s2[i] = n2+48;
}
s1[i] = '\0';
s2[i] = '\0';
a = string(s1);
b = string(s2);
c = add(a, b);
len = c.length();
if(len>n){
for(i=len-n; i<len; i++)
cout << c[i];
printf("\n");
}else{
cout << c << "\n";
}
if(t)printf("\n");
}
return 0;
}