-
Notifications
You must be signed in to change notification settings - Fork 0
/
10106 -Product.cpp
140 lines (117 loc) · 2.69 KB
/
10106 -Product.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
#include<iostream>
#include<string>
#include<sstream>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;
int carry(int sum)
{
return sum/10;
}
int digit(int sum)
{
return sum%10;
}
string add (vector<string>BigInteger)
{
int maxLength = 0;
for(int i = 0; i < BigInteger.size(); i++)
{
if(BigInteger[i].length() >= maxLength) maxLength = BigInteger[i].length();
}
for(int i = 0; i < BigInteger.size(); i++)
{
int l = BigInteger[i].length();
for(int j = 0; j < maxLength-l; j++)
{
BigInteger[i] = "0" + BigInteger[i];
}
}
int hateAche = 0;
string result;
for(int i = maxLength-1; i >= 0; i--)
{
int sum = hateAche;
for(int j = 0; j < BigInteger.size(); j++)
{
int d = BigInteger[j][i] - '0';
sum += d;
}
hateAche = carry(sum);
char ch = digit(sum) + '0';
result = ch + result;
}
if(hateAche > 0)
{
stringstream ss;
ss << hateAche;
string hateAcheString;
ss >> hateAcheString;
result = hateAcheString + result;
}
return result;
}
string multiply(string str, char ch)
{
int hateAche = 0;
int mul = ch - '0';
string result;
for(int i = str.length()-1; i >= 0; i--)
{
int dig = str[i] - '0';
dig = dig*mul + hateAche;
ch = digit(dig) + '0';
hateAche = carry(dig);
result = ch + result;
}
if(hateAche > 0)
{
stringstream ss;
ss << hateAche;
string hateAcheString;
ss >> hateAcheString;
result = hateAcheString + result;
}
return result;
}
string removeLeadingZero(string str)
{
string a;
for(int i = 0; i < str.length(); i++)
{
if(str[i] == '0') str[i] = ' ';
else break;
}
for(int i = 0; i < str.length(); i++)
{
if(str[i] == ' ');
else a += str[i];
}
if(a.length() == 0) return "0";
else return a;
}
int main (void)
{
string a, b;
while(cin >> a >> b)
{
int n = 0;
vector<string> BigInteger;
for(int i = b.length()-1; i >= 0; i--)
{
string mul = multiply(a, b[i]);
for(int j = 0; j < n; j++)
{
char ch = '0';
mul += ch;
}
n++;
BigInteger.push_back(mul);
}
string res = add(BigInteger);
res = removeLeadingZero(res);
cout << res << endl;
}
return 0;
}