-
Notifications
You must be signed in to change notification settings - Fork 0
/
1381.cpp
71 lines (70 loc) · 1.03 KB
/
1381.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
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
bool bigger(string s1, string s2)
{
if(s1.length() > s2.length())
return true;
else if(s1.length() == s2.length() && s1 >= s2)
return true;
else
return false;
}
string clzero(string s)
{
while(s.length() > 0 && s[0]=='0')
s.erase(0,1);
if(s=="")
s="0";
return s;
}
string addition(string s1, string s2)
{
string result;
int i;
while(s1.length() < s2.length())
s1='0'+s1;
while(s2.length() < s1.length())
s2='0'+s2;
s1='0'+s1;
s2='0'+s2;
for(int i=s1.length()-1; i>=0; i--)
{
s1[i]=s1[i]+s2[i]-'0';
if(s1[i]>'9')
{
s1[i]-=10;
s1[i-1]++;
}
}
result=clzero(s1);
return result;
}
string multiply(string s1, string s2)
{
int i;
char c;
string result="0";
for(i=s2.length()-1; i>=0; i--)
{
for(c='1'; c<=s2[i]; c++)
result=addition(result,s1);
s1=s1+'0';
}
result=clzero(result);
return result;
}
int main()
{
int t;
cin>>t;
while(t--)
{
string a,b;
cin>>a>>b;
string c=multiply(a,b);
cout<<c<<endl;
}
return 0;
}