Trong Python, bạn có thể tính toán thương số với
q, mod = divmod[10, 3]
print[q, mod]
# 3 1
1 và phần còn lại với q, mod = divmod[10, 3]
print[q, mod]
# 3 1
2.q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
Chức năng tích hợp
q, mod = divmod[10, 3]
print[q, mod]
# 3 1
3 rất hữu ích khi bạn muốn cả thương số và phần còn lại.- Chức năng tích hợp - DivMod [] - Python 3.7.4 Tài liệu
q, mod = divmod[10, 3]
print[q, mod]
# 3 1
4 Trả về một tuple q, mod = divmod[10, 3]
print[q, mod]
# 3 1
5.Bạn có thể giải nén và gán cho từng biến.
- Giải nén một tuple và danh sách trong Python
q, mod = divmod[10, 3]
print[q, mod]
# 3 1
Tất nhiên, bạn có thể nhận nó như một tuple.
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
Làm thế nào để bạn xác định sự phân chia trong Python?
Trong Python 3. X, toán tử Slash ["/"] thực hiện phân chia thực sự cho tất cả các loại bao gồm các số nguyên, và do đó, ví dụ: 3/2 == 1.5. Kết quả là loại điểm nổi ngay cả khi cả hai đầu vào đều là số nguyên: 4/2 mang lại 2.0.
Làm thế nào để bạn viết bộ phận trong Python?
Làm thế nào để bạn xác định sự phân chia trong Python?
Trong Python 3. X, toán tử Slash ["/"] thực hiện phân chia thực sự cho tất cả các loại bao gồm các số nguyên, và do đó, ví dụ: 3/2 == 1.5. Kết quả là loại điểm nổi ngay cả khi cả hai đầu vào đều là số nguyên: 4/2 mang lại 2.0.
Làm thế nào để bạn viết bộ phận trong Python?
Trong Python, có hai loại toán tử phân chia: /: chia số bên trái cho số bên phải của nó và trả về giá trị điểm nổi. //: Chia số ở bên trái cho số bên phải của nó, làm tròn câu trả lời và trả về một số toàn bộ.
Examples:
Xem thảo luậnCải thiện bài viếtNaive approach
Lưu bài viết[//] operator and remainder using the modulus [%] operator.
Example:
Python3
q, mod = divmod[10, 3]
print[q, mod]
# 3 1
6 q, mod = divmod[10, 3]
print[q, mod]
# 3 1
7q, mod = divmod[10, 3]
print[q, mod]
# 3 1
8q, mod = divmod[10, 3]
print[q, mod]
# 3 1
9answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
0 answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
1answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
22222224q, mod = divmod[10, 3]
print[q, mod]
# 3 1
8answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
6answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
7answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
8answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
9q, mod = divmod[10, 3]
print[q, mod]
# 3 1
8Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 41
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
0 answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
1q, mod = divmod[10, 3]
print[q, mod]
# 3 1
2answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
4q, mod = divmod[10, 3]
print[q, mod]
# 3 1
8answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
6answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
7Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 49
Quotient: 3 Remainder 1 Quotient: 19 Remainder 40
Quotient: 3 Remainder 1 Quotient: 19 Remainder 41
Quotient: 3 Remainder 1 Quotient: 19 Remainder 42
Quotient: 3 Remainder 1 Quotient: 19 Remainder 43
Quotient: 3 Remainder 1 Quotient: 19 Remainder 44
Quotient: 3 Remainder 1 Quotient: 19 Remainder 45
Quotient: 3 Remainder 1 Quotient: 19 Remainder 41
Quotient: 3 Remainder 1 Quotient: 19 Remainder 47
Quotient: 3 Remainder 1 Quotient: 19 Remainder 43
Quotient: 3 Remainder 1 Quotient: 19 Remainder 49
Quotient: 3 Remainder 1 Quotient: 19 Remainder 45
Output:
Quotient: 3 Remainder 1 Quotient: 19 Remainder 4
Độ phức tạp về thời gian: O [1] O[1]
Không gian phụ trợ: O [1] O[1]
Phương pháp 2: Sử dụng phương thức divMod []Using divmod[] method
Phương thức divMod [] lấy hai số làm tham số và trả về tuple chứa cả thương số và phần còn lại.
Example:
Python3
Quotient: 3 Remainder 1 Quotient: 19 Remainder 41
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
0 Quotient: 3 Remainder 1 Quotient: 19 Remainder 43
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
7Quotient: 3 Remainder 1 Quotient: 19 Remainder 42
Quotient: 3 Remainder 1 Quotient: 19 Remainder 43
Quotient: 3 Remainder 1 Quotient: 19 Remainder 444545
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
6answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
7answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
8answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
9answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
6answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
71 % 1 = 0 [1 times 1 plus 0]
1 % 2 = 1 [2 times 0 plus 1]
1 % 3 = 1 [3 times 0 plus 1]
6 % 3 = 0 [3 times 2 plus 0]
7 % 3 = 1 [3 times 2 plus 1]
8 % 3 = 2 [3 times 2 plus 2]
etc
5Quotient: 3 Remainder 1 Quotient: 19 Remainder 40
Quotient: 3 Remainder 1 Quotient: 19 Remainder 41
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
0 Quotient: 3 Remainder 1 Quotient: 19 Remainder 43
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
7Quotient: 3 Remainder 1 Quotient: 19 Remainder 47
Quotient: 3 Remainder 1 Quotient: 19 Remainder 43
Quotient: 3 Remainder 1 Quotient: 19 Remainder 49
Quotient: 3 Remainder 1 Quotient: 19 Remainder 45
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
6answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
7answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
8answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
9answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
6answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
71 % 1 = 0 [1 times 1 plus 0]
1 % 2 = 1 [2 times 0 plus 1]
1 % 3 = 1 [3 times 0 plus 1]
6 % 3 = 0 [3 times 2 plus 0]
7 % 3 = 1 [3 times 2 plus 1]
8 % 3 = 2 [3 times 2 plus 2]
etc
5Quotient: 3 Remainder 1 Quotient: 19 Remainder 40
Output:
Quotient: 3 Remainder 1 Quotient: 19 Remainder 4
Độ phức tạp về thời gian: O [1] O[1]
Không gian phụ trợ: O [1] O[1]
Modulo được thực hiện trong bối cảnh số nguyên, không phải phân số [phần còn lại là số nguyên]. Vì vậy:
1 % 1 = 0 [1 times 1 plus 0]
1 % 2 = 1 [2 times 0 plus 1]
1 % 3 = 1 [3 times 0 plus 1]
6 % 3 = 0 [3 times 2 plus 0]
7 % 3 = 1 [3 times 2 plus 1]
8 % 3 = 2 [3 times 2 plus 2]
etc
Làm cách nào để có được phần còn lại thực tế của x / y?
Bằng cách đó tôi cho rằng bạn có nghĩa là thực hiện một phân chia điểm nổi thông thường?
for i in range[2, 11]:
print 1.0 / i
Đã trả lời ngày 4 tháng 2 năm 2009 lúc 1:10Feb 4, 2009 at 1:10
CodelogicCodelogiccodelogic
70K9 Huy hiệu vàng58 Huy hiệu bạc54 Huy hiệu đồng9 gold badges58 silver badges54 bronze badges
2
Tôi nghĩ rằng bạn có thể nhận được kết quả bạn muốn bằng cách làm một cái gì đó như thế này:
for i in range[2, 11]:
print 1.0*[1 % i] / i
Điều này tính toán phần còn lại [số nguyên] như được giải thích bởi người khác. Sau đó, bạn chia cho mẫu số một lần nữa, để tạo ra phần phân số của thương số.
Lưu ý rằng tôi nhân kết quả của hoạt động modulo với 1.0 để đảm bảo rằng hoạt động phân chia điểm nổi được thực hiện [thay vì phân chia số nguyên, sẽ dẫn đến 0].
Đã trả lời ngày 4 tháng 2 năm 2009 lúc 1:37Feb 4, 2009 at 1:37
Greg Hewgillgreg HewgillGreg Hewgill
911K178 Huy hiệu vàng1131 Huy hiệu bạc1267 Huy hiệu đồng178 gold badges1131 silver badges1267 bronze badges
Bạn đã nhầm lẫn phân chia và mô đun.
"0,5, 0,333333, 0,25", v.v. như tôi mong đợi [1/2 = 0,5, v.v.]. "
Đó là kết quả của sự phân chia.
Không phải mô đun.
Mô đun [
q, mod = divmod[10, 3]
print[q, mod]
# 3 1
2] là phần còn lại còn lại sau khi phân chia số nguyên.Giá trị mẫu của bạn là phân chia đơn giản, đó là toán tử
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
2. Không phải nhà điều hành q, mod = divmod[10, 3]
print[q, mod]
# 3 1
2.Đã trả lời ngày 4 tháng 2 năm 2009 lúc 2:08Feb 4, 2009 at 2:08
S.LottS.LottS.Lott
377K78 Huy hiệu vàng503 Huy hiệu bạc771 Huy hiệu Đồng78 gold badges503 silver badges771 bronze badges
Sẽ không chia 1 cho một số lớn hơn nó dẫn đến 0 với phần còn lại 1?
Các nhà lý thuyết số trong đám đông có thể sửa tôi, nhưng tôi nghĩ mô đun/phần còn lại chỉ được xác định trên các số nguyên.
Đã trả lời ngày 4 tháng 2 năm 2009 lúc 1:11Feb 4, 2009 at 1:11
DanadanaDana
31K17 Huy hiệu vàng62 Huy hiệu bạc72 Huy hiệu đồng17 gold badges62 silver badges72 bronze badges
3
Chúng ta có thể có 2 loại phân chia, mà chúng ta có thể xác định thông qua các loại trả về:
Phao: a/b. Ví dụ: 3/2 = 1.5
def division[a,b]:
return a/b
Int: a // b và a%b. Ví dụ: 3 // 2 = 1 và 3%2 = 1
q, mod = divmod[10, 3]
print[q, mod]
# 3 1
0Đã trả lời ngày 13 tháng 6 năm 2017 lúc 11:00Jun 13, 2017 at 11:00