-
Notifications
You must be signed in to change notification settings - Fork 1
/
10106 Product-j3TByGHV.txt
106 lines (99 loc) · 2.1 KB
/
10106 Product-j3TByGHV.txt
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
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<fstream>
using namespace std;
string add(string s, string str)
{
string sum = "";
int len1 = s.length();
int len2 = str.length();
reverse(s.begin(),s.end());
reverse(str.begin(),str.end());
int x = len1<=len2 ? len1 : len2;
int carry = 0;int a,b,i;
for(i=0; i<x; i++){
a = s[i]-48;
b = str[i]-48;
int p = a+b+carry;
if(p>=10){
carry = 1;
p%=10;
}
else
carry = 0;
sum+=(p+48);
}
if(len1>len2){
for(i; i<len1; i++){
a = s[i]-48;
int p = a+carry;
if(p>=10){
carry = 1;
p%=10;
}
else
carry = 0;
sum+=(p+48);
}
}
if(len1<len2){
for(i; i<len2; i++){
a = str[i]-48;
int p = a+carry;
if(p>=10){
carry = 1;
p%=10;
}
else
carry = 0;
sum+=(p+48);
}
}
if(carry == 1)
sum+=(carry+48);
reverse(sum.begin(),sum.end());
return sum;
}
string normalize(string s)
{
if(s[0]!='0')
return s;
int len = s.size();
reverse(s.begin(),s.end());
for(int i=len-1; i>0 && s[i]=='0'; i--){
s.erase(s.begin() + i);
}
if(s.size()==0)
s = "0";
else
reverse(s.begin(),s.end());
return s;
}
string multiply(string a, string b)
{
string product = "";
int len = a.size() - 1 ;
while(len>=0){
int p = a[len]-48;
int i;
string temp = "0";
for(i=1; i<=p; i++){
temp = add(temp, b);
}
product = add(product , temp);
b = b + "0";
len--;
}
product = normalize(product);
return product;
}
int main()
{
string a,b;
while(cin>>a>>b){
cout<<multiply(a,b)<<endl;
}
return 0;
}