forked from debugster/UVa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva-10013.cpp
181 lines (143 loc) · 3.91 KB
/
uva-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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/// RUN TIME : 0.170s
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000000;
char A[MAX];
char B[MAX];
char C[MAX];
int LenA;
int LenB;
int LenC;
void bigAddition();
int main()
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
int test, M, i;
char dump;
scanf("%d", &test);
while (test--) {
scanf("%d", &M);
dump = getchar();
for (i = 0; i < M; i++) {
scanf("%c %c", &A[i], &B[i]);
dump = getchar();
}
A[M] = B[M] = '\0';
//printf("%s %s\n", A, B);
bigAddition();
if (test != 0) {
printf("\n");
}
}
return 0;
}
void bigAddition()
{
int LenDif, i;
LenA = strlen(A);
LenB = strlen(B);
/*
/// We assume that, A is longer than or equal to B
/// So, if B is longer than A, we have to swap A and B
if (LenB > LenA) {
char *temp = (char *) malloc(LenB * sizeof(char));
strcpy(temp, A);
strcpy(A, B);
strcpy(B, temp);
/// So, we have to swap LenA and LenB too
int tempLen = LenA;
LenA = LenB;
LenB = tempLen;
delete temp;
}
/// We have to shift the shorter number to the right
/// Because, we perform addition bit by bit starting from the rightmost bit
/// Difference between lengths
LenDif = LenA - LenB;
/// If LenDif is 0, we don't need any shifting operation
if (LenDif != 0) {
for (i = LenA - 1; i >= LenDif; i--) {
B[i] = B[i - LenDif];
}
/// Putting a null character to the end and calculate LenB again
B[LenA] = '\0';
/// LenB = strlen(B);
LenB = LenA; /// /// Because, we know it for sure, need not call strlen()
/// Shifting Done !
/// Now, we have to put 0s in first LenDif bits of B
for (i = 0; i < LenDif; i++) {
B[i] = '0';
}
}
*/
/*
/// We'are going to put the result in C
/// C will have either the length of LenA or LenA + 1
/// So, putting a trailing 0 in both A and B will make our calculation easier
/// Now, we are going to shift A and B 1-bit to the right and place a 0 at the first bit
for (i = LenA; i > 0; i--) {
A[i] = A[i - 1];
B[i] = B[i - 1];
}
/// Putting a 0 to the first bit
A[0] = '0';
B[0] = '0';
/// Putting a null character to the end and calculate LenA and LenB again
A[LenA + 1] = '\0';
B[LenB + 1] = '\0';
/// LenA = strlen(A);
/// LenB = strlen(B);
LenA++; /// Because, we know it for sure, need not call strlen()
LenB++;
*/
/// Now, it is the time to perform addition
int sum, carry = 0;
/// Since, we put trailing 0 in both A and B, C will have maximum length of LenA
C[LenA] = '\0';
for (i = LenA - 1; i >= 1; i--) {
sum = A[i] - '0' + B[i] - '0' + carry;
if (sum > 9) {
C[i] = (sum % 10) + '0';
carry = sum / 10;
}
else {
C[i] = sum + '0';
carry = 0;
}
}
sum = A[0] - '0' + B[0] - '0' + carry;
if (sum > 9) {
C[0] = (sum % 10) + '0';
carry = sum / 10;
}
else {
C[0] = sum + '0';
carry = 0;
}
/*
/// If C have trailing 0, Lets remove it
if (C[0] == '0') {
for (i = 0; i < LenA - 1; i++) {
C[i] = C[i + 1];
}
C[LenA - 1] = '\0';
}
*/
/*if (C[0] == '0') {
i = 1;
while (i < LenA) {
printf("%c", C[i++]);
}
printf("\n");
}
else {
printf("%s\n", C);
}*/
if (carry == 0) {
printf("%s\n", C);
}
else {
printf("%d%s\n", carry, C);
}
}