Phân số thích hợp
Đưa ra hai giá trị phân số A/C, B/D trong đó A, B là số và C, D là mẫu số bạn yêu cầu thêm hai giá trị phân số, nếu tổng cho phân số thích hợp để in phần đầu ra nếu nó không phù hợp, hãy chuyển đổi nó thành một phần hỗn hợp và in nó nếu nó là một số toàn bộ, in nó
Dòng đầu vào thứ nhất chứa hai chuỗi phân tách không gian A/C, B/D
input:
1/2 1/4
o/p:
3/4
i/p:
1/2 3/4
o/p:
11/4 [có nghĩa là 1 [1/4]]
i/p:
1/3 2/3
o/p:
1
def validate[data]:
values = data.split['/']
if len[values] == 2 and all[i.isdigit[] for i in values]:
if len[values] > 1 and int[values[1]] == 0:
print["Invalid fraction"]
return True
else:
return False
else:
return True
def split[data]:
values = data.split['/']
return values
def nsd[a, b]:
while a*b != 0:
if a >= b:
a = a % b
else:
b = b % a
return a + b
def main[]:
switch = '1'
fractions = ['1', '2']
you_fraction_split = []
for i in range[2]:
while validate[fractions[i]] or switch == '1':
data = input["Input a fractions: "]
fractions = data.split[' ']
switch = '0'
you_fraction_split.append[split[fractions[i]]]
a = int[you_fraction_split[0][0]]
b = int[you_fraction_split[0][1]]
c = int[you_fraction_split[1][0]]
d = int[you_fraction_split[1][1]]
nsk = b * d // nsd[b, d]
numerator = [int[a * [nsk/b]]] + [int[c * [nsk/d]]]
denominator = nsk
if numerator > denominator:
print[str[numerator] + '/' + str[denominator] + '[that means ' + str[numerator//denominator] + '[' + str[numerator % denominator] + '/' + str[nsk] + ']]']
elif numerator == denominator:
print[int[numerator/denominator]]
else:
print[str[numerator] + '/' + str[denominator]]
if __name__ == '__main__':
main[]
Tìm hiểu thêm về sự giúp đỡ của chúng tôi với các bài tập: Python
using
0using
1 using
39
using
3using
43 using
44
using
3using
43 using
47
using
3using
49using
9 {
2
#include
0std;
9 using
73
#include
18 void
4 void
using
78using
43using
80
a b/c nếu kết quả là một phần không phù hợp hoặc med
A nếu kết quả là một số toàn bộ
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
ĐọcN, the task is to print all proper fractions such that the denominator is less than or equal to N.
Bàn luận A fraction is said to be a proper fraction if the numerator is less than the denominator.
Examples:
Đưa ra một số nguyên n, nhiệm vụ là in tất cả các phân số thích hợp sao cho mẫu số nhỏ hơn hoặc bằng N. & NBSP; N = 3
Output: 1/2, 1/3, 2/3Phân số thích hợp: Một phân số được cho là một phần thích hợp nếu tử số nhỏ hơn mẫu số. & NBSP; & nbsp; N = 4
Output: 1/2, 1/3, 1/4, 2/3, 3/4
Đầu vào: n = 3 & nbsp; đầu ra: 1/2, 1/3, 2/3
Traverse all numerators over
[1, N-1] and, for each of them, traverse over all denominators in the range [numerator+1, N] and check if the numerator and denominator are coprime or not. If found to be coprime, then print the fraction.
Đầu vào: n = 4 & nbsp; đầu ra: 1/2, 1/3, 1/4, 2/3, 3/4 & nbsp;
C++14
#include
Cách tiếp cận: & nbsp; Traverse tất cả các tử số trên [1, n-1] và, đối với mỗi người, đi qua tất cả các mẫu số trong phạm vi [tử số+1, n] và kiểm tra xem tử số và mẫu số có hợp tác hay không. Nếu được tìm thấy là coprime, sau đó in phân số.
Dưới đây là việc thực hiện phương pháp trên: & nbsp;
{
using
namespace
std;
void
printFractions[
int
n]
#include
0____11 #include
2int
#include
4
using
3using
4
using
3using
6
#include
5#include
1 #include
2int
#include
9
using
0namespace
4
#include
5namespace
4
#include
0namespace
4
namespace
4
using
0using
1 using
2
{
#include
0__ std;
5
#include
0std;
7
#include
0std;
7
namespace
4
JavaScript
#include
34#include
1 #include
2using
94 #include
4
#include
54#include
1 #include
2using
94 #include
9
{
#include
0std;
9 namespace
23
#include
0{
#include
34namespace
4#include
94{
#include
5{
#include
0std;
9 namespace
30
using
0{
using
3n]
6
using
3n]
8
Độ phức tạp về thời gian: O [N2 log n] Không gian phụ trợ: O [1] & nbsp;
{
3{
4namespace
1{
6
using
0namespace
4
#include
5namespace
4
Python có thể xử lý phân số không?
namespace
4
void
4 int
#include
06int
#include
08int
#include
10
{
Từ Python 3.2 trở đi, bạn cũng có thể xây dựng một thể hiện phân số trực tiếp từ một số thập phân. Ví dụ thập phân.
namespace
4
Làm thế nào để bạn mã hóa phân số trong Python?
{
Phân số lớp. Phân số [Chuỗi]: Điều này yêu cầu thể hiện chuỗi hoặc Unicode và một phiên bản phân số có cùng giá trị được trả về. Biểu mẫu cho trường hợp này: [dấu hiệu] tử số ['/' mẫu số] Ở đây, dấu biểu thị '+' hoặc '-' và tử số và mẫu số là các chuỗi của một chữ số.
#include
0std;
7
namespace
4
namespace
4
Python3
Làm thế nào để bạn thêm phân số với các mẫu số khác nhau trong Python?
Khởi tạo các biến của tử số và mẫu số ..
Lấy đầu vào người dùng của hai phân số ..
Tìm tử số bằng điều kiện này [N1*D2] +[D1*N2] trong đó N1, N2 là tử số và D1 và D2 là mẫu số ..
Tìm mẫu số bằng điều kiện này [D1*D2] cho LCM ..
Tính GCD của một tử số và mẫu số mới này ..
Xem thảo luận
#include
32 #include
82
#include
34using
1 #include
67#include
57#include
57 #include
15#include
60
#include
0std;
9 #include
92
#include
34#include
94#include
60
____10std;
9 #include
98#include
99 #include
10
using
1 using
02#include
57#include
57using
05__160
#include
34using
08#include
57 #include
26
#include
34using
12
C#
using
using
14
void
2 void
3
void
4 void
printFractions[
int
n]
{
____10#include
1#include
2int
using
27
#include
0{
#include
5#include
1#include
2int
using
34
#include
5{
using
0using
1 using
39
using
0{
using
3using
43 using
44
using
3using
43 using
47
using
3using
49using
9 {
2
using
52{
4namespace
1{
6
using
0namespace
4
#include
5namespace
4
#include
0namespace
4
namespace
4
void
4 int
#include
06int
#include
08int
#include
10
{
#include
0std;
9 using
73
namespace
4
#include
18 void
4 void
using
78using
43using
80
{
#include
0__ std;
5
#include
0std;
7
namespace
4
namespace
4
JavaScript
using
89
using
90
#include
34#include
1 #include
2using
94 #include
4
#include
54#include
1 #include
2using
94 #include
9
#include
54using
1namespace
03
#include
5namespace
05
#include
5namespace
07
#include
5namespace
09
#include
54namespace
4
#include
0namespace
4
#include
34namespace
4
namespace
4
namespace
17
#include
34using
1namespace
20
#include
0std;
9 namespace
23
#include
34namespace
4#include
94{
#include
0std;
9 namespace
30
#include
34namespace
4
namespace
4
namespace
34
std;
7
namespace
36
Độ phức tạp về thời gian: O [N2 log n] Không gian phụ trợ: O [1] & nbsp; O[N2 log N]
Auxiliary Space: O[1]