Tôi chỉ bắt đầu sử dụng Python, vì vậy tôi không biết rõ về nó. Bạn có thể giúp làm thế nào để nói rằng con số không nên chia hết bởi một số nguyên khác do tôi đưa ra không? Ví dụ, nếu A không chia hết cho 2.
Đã hỏi ngày 19 tháng 11 năm 2015 lúc 20:33Nov 19, 2015 at 20:33
5
Kiểm tra nhà điều hành %.
if x%2 == 0: # x is divisible by 2 print xĐã trả lời ngày 19 tháng 11 năm 2015 lúc 20:35Nov 19, 2015 at 20:35
>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False
Đã trả lời ngày 19 tháng 11 năm 2015 lúc 20:36Nov 19, 2015 at 20:36
R narr narR Nar
5.3851 Huy hiệu vàng16 Huy hiệu bạc31 Huy hiệu đồng1 gold badge16 silver badges31 bronze badges
"A không chia hết cho 2": (a % 2) != 0
Đã trả lời ngày 19 tháng 11 năm 2015 lúc 20:35Nov 19, 2015 at 20:35
200_success200_success200_success
Đã trả lời ngày 19 tháng 11 năm 2015 lúc 20:361 gold badge42 silver badges71 bronze badges
Chương trình Python để có được một số Num và kiểm tra xem Num có chia hết cho 3 không.
Mẫu đầu vào 1:
27
Đầu ra mẫu 1:
Chia hết cho 3
Mẫu đầu vào 2:
43
Đầu ra mẫu 2:
Không chia hết cho 3
Chương trình hoặc giải pháp
num1=int(input("Enter your number:")) if(num1%3==0): print("{} is divisible by 3".format(num1)) else: print("{} is not divisible by 3".format(num1))
Giải thích chương trình
Nhận Num đầu vào từ người dùng bằng phương thức Input () Kiểm tra xem phần còn lại của num chia cho 3 bằng 0 bằng cách sử dụng câu lệnh IF.
Nếu là 0, thì in num là chia hết cho 3 bằng phương thức print ().
Khác in num không chia hết cho 3 bằng phương thức print ().
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
Đọc
Example:
Input: 10 Output: Numbers not divisible by 2 and 3 1 5 7Bàn luận: We check if the number is not divisible by 2 and 3 using the and clause, then outputs the number.
Python3
Chúng ta có thể nhập một tập hợp số nguyên và kiểm tra số nguyên nào trong phạm vi này, bắt đầu với 1 không chia hết cho 2 hoặc 3, bằng cách kiểm tra phần còn lại của số nguyên với 2 và 3. & nbsp;
Phương pháp 1: Chúng tôi kiểm tra xem số không chia hết cho 2 và 3 bằng mệnh đề và điều khoản, sau đó xuất số. & NBSP;
>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 5>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 6>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 7>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 8
max_num ____10 >>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 1
>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 2>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 0 >>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 4
Input: 10 Output: Numbers not divisible by 2 and 3 1 5 79>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 5Numbers not divisible by 2 and 3 1 5 7 11 13 17 191
>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 9 num1=int(input("Enter your number:")) if(num1%3==0): print("{} is divisible by 3".format(num1)) else: print("{} is not divisible by 3".format(num1)) 0>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 0 num1=int(input("Enter your number:")) if(num1%3==0): print("{} is divisible by 3".format(num1)) else: print("{} is not divisible by 3".format(num1)) 2
Output:
Numbers not divisible by 2 and 3 1 5 7 11 13 17 19Độ phức tạp về thời gian: O (1) O(1)
Không gian phụ trợ: O (1) O(1)
Làm thế nào để bạn không sử dụng chia sẻ trong Python?: We traverse the odd numbers starting with 1 since even numbers are divisible by 2. So, we increment the for a loop by 2, traversing only odd numbers and check, which one of them is not divisible by 3. This approach is better than the previous one since it only iterates through half the number of elements in the specified range.
Mã python sau đây minh họa điều này: & nbsp;
Python3
max_num ____10 Numbers not divisible by 2 or 3 : 1 5 7 11 13 17 19 23 25 29 31 35 370
>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 5>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 6Numbers not divisible by 2 or 3 : 1 5 7 11 13 17 19 23 25 29 31 35 373>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 8
Numbers not divisible by 2 or 3 : 1 5 7 11 13 17 19 23 25 29 31 35 375 Numbers not divisible by 2 or 3 : 1 5 7 11 13 17 19 23 25 29 31 35 376Numbers not divisible by 2 or 3 : 1 5 7 11 13 17 19 23 25 29 31 35 377 Numbers not divisible by 2 or 3 : 1 5 7 11 13 17 19 23 25 29 31 35 378>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 6>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 4(a % 2) != 01 num1=int(input("Enter your number:")) if(num1%3==0): print("{} is divisible by 3".format(num1)) else: print("{} is not divisible by 3".format(num1)) 7(a % 2) != 03
num1=int(input("Enter your number:")) if(num1%3==0): print("{} is divisible by 3".format(num1)) else: print("{} is not divisible by 3".format(num1)) 3 num1=int(input("Enter your number:")) if(num1%3==0): print("{} is divisible by 3".format(num1)) else: print("{} is not divisible by 3".format(num1)) 4 Numbers not divisible by 2 or 3 : 1 5 7 11 13 17 19 23 25 29 31 35 376 num1=int(input("Enter your number:")) if(num1%3==0): print("{} is divisible by 3".format(num1)) else: print("{} is not divisible by 3".format(num1)) 6 Input: 10 Output: Numbers not divisible by 2 and 3 1 5 74 num1=int(input("Enter your number:")) if(num1%3==0): print("{} is divisible by 3".format(num1)) else: print("{} is not divisible by 3".format(num1)) 8>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 0
Input: 10 Output: Numbers not divisible by 2 and 3 1 5 79>>> def isDivis(divisor,num): return not num%divisor >>> isDivis(2,12) True >>> isDivis(2,21) False >>> isDivis(3,21) True >>> isDivis(3,14) False 5 5
Output:
Numbers not divisible by 2 or 3 : 1 5 7 11 13 17 19 23 25 29 31 35 37Độ phức tạp về thời gian: O (1) O(1)
Không gian phụ trợ: O (1) O(1)