-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArraysSumOf2Arrays.cpp
66 lines (64 loc) · 1.08 KB
/
ArraysSumOf2Arrays.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
#include <bits/stdc++.h>
using namespace std;
int calSum(int a[], int b[], int n, int k)
{
int t[n];
int i = n - 1, j = k - 1, q = n - 1;
int sum = 0, carry = 0;
while (j >= 0)
{
sum = a[i] + b[j] + carry;
t[q] = (sum % 10);
carry = sum / 10;
q--;
j--;
i--;
}
if (n > k)
{
while (i >= 0)
{
sum = a[i] + carry;
t[k] = (sum % 10);
carry = sum / 10;
q--;
i--;
}
}
int ans = 0;
if (carry)
ans = 10;
for (int i = 0; i < n; i++)
{
ans += t[i];
ans *= 10;
}
return ans / 10;
}
int calSum6(int a[], int b[], int n, int k)
{
if (n >= k)
return calSum(a, b, n, k);
else
return calSum(b, a, k, n);
}
int main()
{
int n;
cin >> n;
int a[100];
int i;
for (i = 0; i < n; i++)
{
cin >> a[i];
}
int k;
cin >> k;
int b[100];
int j;
for (j = 0; j < k; j++)
{
cin >> b[j];
}
cout << calSum6(a, b, n, k);
}