-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubtract.cpp
129 lines (81 loc) · 1.31 KB
/
subtract.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
#include<bits/stdc++.h>
using namespace std;
string add (string num1, string num2)
{
int l1 = num1.length() - 1;
int l2 = num2.length() - 1;
if(l1 <= l2)
{
for(int i=l2; i>=l2-l1; i--)
num1[i] = num1[i-l2+l1];
for(int j=l2-l1-1; j>=0; j--)
num1[j] = '0';
l1 = l2;
num1[l1+1] = '\0';
}
else
{
for(int i=l1; i>=l1-l2; i--)
num2[i] = num2[i-l1+l2];
for(int j=l1-l2-1; j>=0; j--)
num2[j] = '0';
l2 = l1;
num2[l2+1] = '\0';
}
string sum = "\0";
int rem = 0;
for(int k=0; k<=l2; k++)
{
sum[k] = num1[l2-k] - 48 + num2[l2-k] - 48 + rem + 48;
if(sum[k]>=58)
{sum[k] -= 10;
rem = 1;}
else
rem = 0;
sum += sum[k];
}
if(rem==1)
{sum += rem + 48;
sum[l1+2] = '\0';}
else
sum[l1+1] = '\0';
reverse(sum.begin(),sum.end());
return sum;
}
string comp(string num2)
{
int l2 = num2.length() - 1;
string num3 = num2;
int i = l2;
while(num2[i]=='0')
{
i--;
}
num2[i] = 106 - num2[i];
i--;
for(; i>=0; i--)
{
num2[i] = 105 - num2[i];
}
return num2;
}
int main() {
string num1, num2;
cin >> num1;
cin >> num2;
int l1 = num1.length() - 1, l2 = num2.length() - 1;
string num3 = num2;
num2 = comp(num2);
string num4 = add(num1, num2);
int l4 = num4.length() - 1;
if(l4 > l2)
{
num4[0] -= 1;
}
else
{
num4 = "-" + comp(num4);
}
cout << num4 << endl;
return 0;
}