-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTDL048.cpp
177 lines (156 loc) · 3.97 KB
/
CTDL048.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Một phân số đơn vị nếu tử số của phân số đó là 1. Mọi phân số nguyên dương đều có thể biểu diễn thành tổng các phân số đơn vị. Ví dụ 2/3 = 1/2 + 1/6. Cho phân số nguyên dương P/Q bất kỳ (P < Q), hãy biểu diễn phân số nguyên dương thành tổng phân số đơn vị.
#include <bits/stdc++.h>
using namespace std;
/*
vector<int> temp;
void uocso(int mauso, int tuso){ // ham lấy các ước của mẫu số bé hơn tử số
for (int i = 1; i < tuso; i++){
if (mauso % i == 0){
temp.push_back(i);
}
}
}
void check(int tuso, int mauso){
queue<int>phansodonvi; // chứa các mẫu số của phân số đơn vị
uocso(mauso, tuso);
for (int i = temp.size() - 1; i >= 0; i--){ // hàm tách thành các phân số đơn vị
if(tuso >= temp[i]){ // vd 13/28 = (2 + 4 + 7)/28 = 1/14 + 1/7 + 1/4; (trừ dần tử số cho các ước số theo thứ tự giảm dần sao cho tuso = 0, nếu tuso đã trừ < ước số đang xét thì bỏ qua xét ước số tiếp theo vd 10/28 thì 10 - 7 - 2 - 1 do 10 - 7 = 3 < 4 bỏ qua 4)
tuso -= temp[i];
phansodonvi.push(mauso / temp[i]);
}
}
while(!phansodonvi.empty()){
cout<<1<<'/'<<phansodonvi.front();
phansodonvi.pop();
if (!phansodonvi.empty()){
cout<<" ";
cout<<'+';
}
cout<<" ";
}
}
int main(){
int t; cin >> t;
while(t--){
int tuso, mauso;
cin >> tuso >> mauso;
if (tuso == 1){
cout<<tuso<<'/'<<mauso;
}else if(mauso < 9 && mauso % 2 != 0){ // vd 3 5 7
mauso *= 2;
tuso *= 2;
check(tuso, mauso);
}else{
check(tuso, mauso);
}
cout<<endl;
}
}
*/
/*
#include <bits/stdc++.h>
using namespace std;
/*
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}*/
/*
int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
void check (int tuso, int mauso){
if (tuso == 1){
cout<<tuso<<'/'<<mauso;
return;
}
int temp = (mauso / tuso) + 1;
cout<<1<<'/'<<temp<<" + ";
tuso = temp*tuso - mauso;
mauso = mauso*temp;
int ucln = gcd(tuso, mauso);
tuso /= ucln;
mauso /= ucln;
check(tuso, mauso);
}
int main(){
int t; cin >> t;
while(t--){
int tuso, mauso;
cin >> tuso >> mauso;
check(tuso, mauso);
cout<<endl;
}
}
*/
/*
int main(){
int t;
cin >> t;
while(t--){
long long a, b, n, m, x;
cin >> a >> b;
while (b % a != 0){
n = a;
m = b;
x = b / a;
cout << 1 << "/" << x + 1 << " + ";
a = (x + 1) * n - m;
b = (x + 1) * b;
}
cout << 1 << "/" << b / a;
cout <<'\n';
}
}
*/
/*
void check (long long tuso, long long mauso){
if (mauso % tuso == 0){
cout << 1 << "/" << mauso / tuso;
return;
}
long long temp = mauso / tuso + 1;
cout << 1 << "/" << temp << " + ";
tuso = temp * tuso - mauso;
mauso = temp * mauso;
check(tuso, mauso);
}
int main(){
int t; cin >> t;
while(t--){
long long tuso, mauso;
cin >> tuso >> mauso;
check(tuso, mauso);
cout<<endl;
}
}
*/
/*
void check (int tuso, int mauso){
if (tuso == 1){
cout<<tuso<<'/'<<mauso;
return;
}
int temp = mauso / tuso + 1;
cout << 1 << '/' << temp << " + ";
tuso = temp*tuso - mauso;
mauso = mauso*temp;
if (mauso % tuso == 0){
cout << 1 << '/' << mauso / tuso;
return;
}
check(tuso, mauso);
}
int main(){
int t; cin >> t;
while(t--){
int tuso, mauso;
cin >> tuso >> mauso;
check(tuso, mauso);
cout<<endl;
}
}
*/