-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCPPLAN03.cpp
More file actions
54 lines (54 loc) · 1.13 KB
/
Copy pathCPPLAN03.cpp
File metadata and controls
54 lines (54 loc) · 1.13 KB
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
#include<bits/stdc++.h>
using namespace std;
string mul(string a, string b){
string res="";
int n=a.length();
int m=b.length();
int len=n+m-1;
int carry=0;
for(int i=len;i>=0;i--){
int tmp=0;
for(int j=n-1;j>=0;j--)
if(i-j<=m && i-j>=1){
int a1=a[j]-48;
int b1=b[i-j-1]-48;
tmp+=a1*b1;
}
tmp+=carry;
carry=tmp/10;
res=(char)(tmp%10 + 48)+res;
}
while(res.length()>1 && res[0]=='0') res.erase(0,1);
return res;
}
void process(){
string a,b;
string res = "";
cin >> a >> b;
if ((a[0] == '-' && b[0] != '-') || (a[0] != '-' && b[0] == '-')){
if (a[0] == '-') a.erase(0,1);
if (b[0] == '-') b.erase(0,1);
res += "-";
res = mul(a,b);
}
else{
if (a[0] == '-') a.erase(0,1);
if (b[0] == '-') b.erase(0,1);
res = mul(a,b);
}
cout << res << endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);cout.tie(NULL);
int T;
cin >> T;
while(T--){
process();
}
return 0;
}