-
Notifications
You must be signed in to change notification settings - Fork 1
/
Division.cpp
103 lines (95 loc) · 1.97 KB
/
Division.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
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
string sub(string s1,string s2){
char a='0';
lli p=0,t;
if(s1.length()<s2.length())
{
swap(s1,s2);
cout<<"-";
}
if(s1.length()==s2.length())
{
for(int i=0;i<s1.length();i++)
{
if(s1[i]>s2[i]) break;
if(s1[i]<s2[i])
{
swap(s1,s2);
cout<<"-";
break;
}
}
}
while(s2.length()<s1.length())
{
s2.insert(0,"0",1);
}
string result="";
for(lli i=s1.length()-1; i>=0 ;i--)
{
t= (int)(s1[i]-'0') - (int)(s2[i] -'0') + p;
p=0;
if(t<0)
{
p=-1;
t+=10;
}
result.push_back((char) (t + (int)a )) ;
}
reverse(result.begin(), result.end());
result.erase(0, result.find_first_not_of('0'));
if(result=="") result="0";
return result;
}
int compare(string s1,string s2)
{
lli i;
if(s1.length()>s2.length()) return 1;
else if(s1.length()<s2.length()) return 0;
else{
for(i=0;i<s2.length();i++)
{
if(s1[i]>s2[i]) return 1;
if(s2[i]>s1[i]) return 0;
}
return 2;
}
}
int main()
{
lli T=0;
cin>>T;
while(T--){
string s1;
cin>>s1;
string s2;
cin>>s2;
string res="";
lli l1=s1.length();
lli l2=s2.length();
if(compare(s1,s2)==0){ cout<<0<<" "<<s1<<endl; return 0;}
if(compare(s1,s2)==2) {cout<<1<<" "<<0<<endl; return 0;}
string x="";
lli j=l2-1,k=0;
for(int i=0;i<l2;i++) x.push_back(s1[i]);
while(j<l1)
{
k=0;
while(compare(x,s2)!=0)
{
x=sub(x,s2);
k++;
}
j++;
if(j<l1) x.push_back(s1[j]);
x.erase(0, x.find_first_not_of('0'));
if(x=="") x="0";
res.push_back(k+'0');
}
res.erase(0, res.find_first_not_of('0'));
cout<<res<<" "<<x<<endl;
}
return 0;
}