在此代码中,我必须从用户处获取输入,并说输入是否可被5整除, 但无需使用 user % 5 == 0
Like this:
def f(x):
return (x % 10) in (0, 5)
Or this:
def g(x):
return x == x // 5 * 5
Or this slow one:
def h(x):
if x < 0:
x = -x
while x >= 5:
x -= 5
return x == 0
Or this (also slow, but faster than h):
def i(x):
return str(x)[-1] in '05'
Or this:
def j(x):
return str(x << 1).endswith('0')
There are many more solutions.
t = int(input())
print(t/5==t//5)
or use
def f(x):
return (t/5==t//5)
this will return true or false if the number is divisible by 5 or not.
PS. as pointed by @Pts this code is valid upto certain length of number(10^15) ,if number is too large floating point error come.
en.wikipedia.org/wiki/IEEE_754#Basic_and_interchange_formats indicates that 64-bit floats have 53-bit precisions, so numbers t whose absolute value is smaller than 2 ** 53 will work, others may fail
def main() :
x = int(input("enter the number"))
if x // 5 * 5 == x :
print("yes")
else:
print("no")
main()
This worked
I naturally think of something like this:
def divisible(user, x):
if (x > user or x <= 0):
return False
while (user > 0):
user -= x
return user == 0
Don't forget to check if x
is negative and lower than user
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.