-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3115.cpp
111 lines (93 loc) · 1.28 KB
/
3115.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
/*
题目:p3115 高精度练习之减法
*/
#include <iostream>
#include <string>
using namespace std;
string BigNumberMinus(string a, string b)
{
string::reverse_iterator arit = a.rbegin();
string::reverse_iterator brit = b.rbegin();
string amb = "";
int c = 0;
int tmp,rel;
int flag,i;
if(a.length()>b.length())
flag = 1;
else
{
if(a.length()<b.length())
flag = 0;
else
{
for(i=0;i<a.length();i++)
{
if(a[i]>b[i])
{
flag = 1;
break;
}
if(a[i]<b[i])
{
flag = 0;
break;
}
}
if(i==a.length())
return "0";
}
}
while(arit!=a.rend()&&brit!=b.rend())
{
if(flag)
tmp = *arit-*brit-c;
else
tmp = *brit-*arit-c;
if(tmp<0)
{
c = 1;
tmp = tmp+10;
}
else
c = 0;
amb.push_back(tmp+'0');
arit++;
brit++;
}
while(arit!=a.rend())
{
tmp = *arit-'0'-c;
if(tmp<0)
c = 1;
else
c = 0;
amb.push_back(tmp+'0');
arit++;
}
while(brit!=b.rend())
{
tmp = *brit-'0'-c;
if(tmp<0)
c = 1;
else
c = 0;
amb.push_back(tmp+'0');
brit++;
}
if(!flag)
amb.push_back('-');
string t = "";
for(i=amb.size()-1;i>=0;i--)
{
t.push_back(amb[i]);
}
return t;
}
int main()
{
string a,b;
cin >> a >> b;
cout << BigNumberMinus(a,b) << endl;
return 0;
}