-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuva-10035.cpp
143 lines (113 loc) · 3.41 KB
/
uva-10035.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
#include <bits/stdc++.h>
using namespace std;
const int MAX = 20;
char A[MAX];
char B[MAX];
char C[MAX];
int LenA;
int LenB;
int LenC;
int bigAddition();
int main()
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
int carry;
while (1) {
scanf("%s %s", A, B);
if (!(strcmp(A, "0")) && !(strcmp(B, "0"))) break;
carry = bigAddition();
if (carry == 0) {
printf("No carry operation.\n");
}
else if (carry == 1) {
printf("1 carry operation.\n");
}
else {
printf("%d carry operations.\n", carry);
}
}
return 0;
}
int 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, countCarry = 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 >= 0; i--) {
sum = A[i] - '0' + B[i] - '0' + carry;
if (sum > 9) {
//C[i] = (sum % 10) + '0';
carry = sum / 10;
countCarry++;
}
else {
//C[i] = 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';
}
*/
return countCarry;
}