-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJULKA.cpp
198 lines (177 loc) · 3.1 KB
/
JULKA.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stdio.h>
#define toDigit(c) (c-'0')
int main()
{
int t=10;
while(t--)
{
int i=0,j=0,end=0;
char total[111],more[111];
scanf("%s",total);
scanf("%s",more);
while(total[i]!='\0')
i++;
i--; //take the pointer to the rightmost digit of total[]
int z;
z=i;
end=i;
while(more[j]!='\0')
j++;
j--; //take the pointer to the rightmost digit of more[]
if(j<i)
{
int tmp;
for(tmp=i; tmp>=0; tmp--)
{
if(j<0)
{
more[tmp]='0';
j--;
}
else
more[tmp]=more[j--]; //This block of code is to make the size of more[] same as that of total[] by appending 0s in
} //the prefix
}
j=i;
int tempold[111],temp1[111],h=0;
for(h=0; h<=z; h++)
{
tempold[h]=toDigit(total[h]);
}
for(h=0; h<=z; h++)
{
temp1[h]=toDigit(more[h]);
}
while(j>=0)
{
if(tempold[i]<temp1[j])
{
tempold[i]=toDigit(total[i])+10;
temp1[j-1]=temp1[j-1]+1;
}
tempold[i]=tempold[i]-temp1[j];
j--;
i--;
}
i=0;
while(i<=z && tempold[i]==0)
{
i++;
}
//for(; i<=z; i++)
//{
//printf("%d",tempold[i]);
//}
//printf("\n");
//Logic of subtraction ends above
//Logic of division begins here:
//z points to the end of the number in the array
//i=0 points to the beginning of the number in the array
int quotient[111];
int dividend=0;
i=0;
bool flag=true;
int m=0;
while(tempold[i]==0)
{
i++;
}
m=i;
while(i--)
{
quotient[i]=0; //Prefix as many zeros in the quotient array as there are in the tempold[] one!!
}
i=m;
int quotienttemp[111];
for(h=0; h<=z; h++)
{
quotienttemp[h]=quotient[h];
}
while (i<=z)
{
if(flag==true && tempold[i]<2)
{
flag=false;
dividend=tempold[i]*10+tempold[i+1];
quotient[i]=0;
quotienttemp[i]=0;
i++;
}
else
{
if(flag==true)
{
flag=false;
dividend=tempold[i];
}
}
quotient[i]=dividend/2;
quotienttemp[i]=dividend/2;
dividend=dividend%2;
if(i==z && dividend==1)
{
quotient[i]++;
}
if(i<z)
dividend=dividend*10+tempold[i+1];
else
break;
i++;
}
//Logic of division ends above
//Below is the copy-pasted logic of subraction:
//i is the end of the total array
//j is the end of the quotient array
//Both the arrays end at z
i=end;
j=z;
//j=i;
int temp[111];
for(h=0; h<=z; h++)
{
temp[h]=toDigit(total[h]);
}
//for(h=0; h<=z; h++)
//{
//printf("The temp array originally is: %d \n",temp[h]);
//}
//int quotienttemp[101];
//for(h=0; h<=z; h++)
//{
//quotienttemp[h]=quotient[h];
//}
while(j>=0)
{
//printf("Comparing %d and %d \n",temp[i],quotient[j]);
if(temp[i]<quotient[j])
{
temp[i]=toDigit(total[i])+10;
quotient[j-1]=quotient[j-1]+1;
}
temp[i]=temp[i]-quotient[j];
j--;
i--;
}
i=0;
while(i<=z && temp[i]==0)
{
i++;
}
for(; i<=z; i++)
{
printf("%d",temp[i]);
}
printf("\n");
i=0;
while(i<=z && quotienttemp[i]==0)
{
i++;
}
for(; i<=z; i++)
{
printf("%d",quotienttemp[i]);
}
printf("\n");
}
return 0;
}