Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Input: n = 10 m = 3 Output: Quotient: 3 Remainder 1 Input n = 99 m = 5 Output: Quotient: 19 Remainder 4
Bàn luậnNaive approach
Cho hai số n và m. Nhiệm vụ là tìm thương số và phần còn lại của hai số bằng cách chia n cho m.[//] operator and remainder using the modulus [%] operator.
Example:
Python3
Phương pháp 1: Cách tiếp cận ngây thơ
Cách tiếp cận ngây thơ là tìm thương số bằng toán tử phân chia kép [//] và còn lại bằng toán tử mô đun [%].
Quotient: 3 Remainder 1 Quotient: 19 Remainder 40
Quotient: 3 Remainder 1 Quotient: 19 Remainder 48
Quotient: 3 Remainder 1 Quotient: 19 Remainder 49
Quotient: 3 Remainder 1 Quotient: 19 Remainder 40
Quotient: 3 Remainder 1 Quotient: 19 Remainder 41
def
find[n, m]:
Quotient: 3 Remainder 1 Quotient: 19 Remainder 40
Quotient: 3 Remainder 1 Quotient: 19 Remainder 48
Quotient: 3 Remainder 1 Quotient: 19 Remainder 49
q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
1q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
2____10
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 44
Quotient: 3 Remainder 1 Quotient: 19 Remainder 46
q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
3q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
9q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
5q, mod = divmod[10, 3]
print[q, mod]
# 3 1
1q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
7Output:
Quotient: 3 Remainder 1 Quotient: 19 Remainder 4
____10Quotient: 3
Remainder 1
Quotient: 19
Remainder 4
3Quotient: 3
Remainder 1
Quotient: 19
Remainder 4
2 Quotient: 3
Remainder 1
Quotient: 19
Remainder 4
3Quotient: 3
Remainder 1
Quotient: 19
Remainder 4
6Quotient: 3
Remainder 1
Quotient: 19
Remainder 4
6 O[1]
3q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
4q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
5q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
6q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
7 O[1]q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
Độ phức tạp về thời gian: O [1]Using divmod[] method
Không gian phụ trợ: O [1]
Example:
Python3
Phương pháp 2: Sử dụng phương thức divMod []
Quotient: 3 Remainder 1 Quotient: 19 Remainder 48
Quotient: 3 Remainder 1 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 48
Quotient: 3 Remainder 1 Quotient: 19 Remainder 49
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
7q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
2Phươ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.
Quotient: 3 Remainder 1 Quotient: 19 Remainder 48
Quotient: 3 Remainder 1 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 48
Quotient: 3 Remainder 1 Quotient: 19 Remainder 49
answer = divmod[10, 3]
print[answer]
print[answer[0], answer[1]]
# [3, 1]
# 3 1
7q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
2Output:
Quotient: 3 Remainder 1 Quotient: 19 Remainder 4
____10Quotient: 3
Remainder 1
Quotient: 19
Remainder 4
3Quotient: 3
Remainder 1
Quotient: 19
Remainder 4
2 Quotient: 3
Remainder 1
Quotient: 19
Remainder 4
3Quotient: 3
Remainder 1
Quotient: 19
Remainder 4
6Quotient: 3
Remainder 1
Quotient: 19
Remainder 4
6 O[1]
3q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
4q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
5q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
6q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
7 O[1]q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
Trong Python, bạn có thể tính toán thương số với
Case 1: Enter the first number: 15 Enter the second number: 7 Quotient is: 2 Remainder is: 1 Case 2: Enter the first number: 125 Enter the second number: 7 Quotient is: 17 Remainder is: 65 và phần còn lại với
Quotient: 3 Remainder 1 Quotient: 19 Remainder 46.
q = 10 // 3
mod = 10 % 3
print[q, mod]
# 3 1
Hàm tích hợp
Case 1: Enter the first number: 15 Enter the second number: 7 Quotient is: 2 Remainder is: 1 Case 2: Enter the first number: 125 Enter the second number: 7 Quotient is: 17 Remainder is: 67 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
Case 1: Enter the first number: 15 Enter the second number: 7 Quotient is: 2 Remainder is: 1 Case 2: Enter the first number: 125 Enter the second number: 7 Quotient is: 17 Remainder is: 68 Trả về một tuple
Case 1: Enter the first number: 15 Enter the second number: 7 Quotient is: 2 Remainder is: 1 Case 2: Enter the first number: 125 Enter the second number: 7 Quotient is: 17 Remainder is: 69.
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
Đây là một chương trình Python để đọc hai số và in thương số và phần còn lại của họ. Mô tả vấn đề Chương trình lấy hai số và in thương số và phần còn lại. Giải pháp vấn đề
1. Lấy số thứ nhất và thứ hai và lưu trữ nó trong các biến riêng biệt. 2. Sau đó lấy thương số bằng cách sử dụng phân chia và phần còn lại bằng toán tử mô đun. 3. Thoát.
2. Then obtain the quotient using division and the remainder using
modulus operator.
3. Exit.
Chương trình/mã nguồn
Dưới đây là mã nguồn của chương trình Python để đọc hai số và in chỉ số và phần còn lại của chúng. Đầu ra chương trình cũng được hiển thị dưới đây.
a=int[input["Enter the first number: "]] b=int[input["Enter the second number: "]] quotient=a//b remainder=a%b print["Quotient is:",quotient] print["Remainder is:",remainder]
Giải thích chương trình
1. Người dùng phải nhập số thứ nhất và thứ hai. 2. Thương số thu được bằng cách sử dụng phân chia thực [// toán tử]. 3. Toán tử mô đun cho phần còn lại khi A được chia cho b.
2. The quotient is obtained using true division [// operator].
3. The modulus operator gives the remainder when a is divided by b.
Trường hợp kiểm tra thời gian chạy
Case 1: Enter the first number: 15 Enter the second number: 7 Quotient is: 2 Remainder is: 1 Case 2: Enter the first number: 125 Enter the second number: 7 Quotient is: 17 Remainder is: 6
Sê -ri Giáo dục & Học tập toàn cầu Sanfoundry - Chương trình Python.
Để thực hành tất cả các chương trình Python, đây là bộ hoàn thành hơn 150 vấn đề và giải pháp Python.
Bước tiếp theo:
- Nhận Giấy chứng nhận miễn phí trong chương trình Python
- Tham gia cuộc thi chứng nhận lập trình Python
- Trở thành một người xếp hạng hàng đầu trong chương trình Python
- Thực hiện các bài kiểm tra lập trình Python
- Các bài kiểm tra thực hành theo chương: Chương 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10
- Các bài kiểm tra giả chương: Chương 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10,
Manish Bhojasia, một cựu chiến binh công nghệ với hơn 20 năm @ Cisco & Wipro, là người sáng lập và CTO tại Sanfoundry. Ông sống ở Bangalore, và tập trung vào sự phát triển của nhân Linux, Công nghệ San, Cvanced C, Cấu trúc dữ liệu & Alogrithms. Giữ kết nối với anh ta tại LinkedIn.Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.
Đăng ký các lớp học chính miễn phí của mình tại YouTube & Thảo luận kỹ thuật tại Telegram SanfoundryClasses.