[英]Find all positive numbers divisible by 10 and less than n
I need to find all positive numbers that are divisible by 10 and less than n, i found a string with the same question but i have a hard time interpreting it as the user was using java so the codes are very different and confusing.我需要找到所有可被 10 整除且小于 n 的正数,我发现了一个具有相同问题的字符串,但我很难解释它,因为用户使用的是 java,因此代码非常不同且令人困惑。
i tried to make a code by piecing together codes i've checked out but it only works if its divisible by other numbers, if its 10 it would keep going on forever with 0.我试图通过将我检查过的代码拼凑在一起来制作一个代码,但它只有在它可以被其他数字整除时才有效,如果它是 10,它将以 0 永远持续下去。
n = int(input("Enter a number: "))
x = 0
while x < n :
r = n % 10
if r % 10 != 0 :
x = x + r
print("positive numbers divisible by 10 ", x)
Below is simpler code which will help to get the list of numbers divisible by 10 and less than n:下面是更简单的代码,它有助于获得可被 10 整除且小于 n 的数字列表:
n = int(input("Enter a number n: "))
divisibleBy10 = []
for i in range(0, n):
if i % 10 == 0:
divisibleBy10.append(i)
print(divisibleBy10)
You can do like this:你可以这样做:
n = 100
i = 0
while i<n:
if i%10==0:
print(i)
i+=1
This code below tries to reduce the number of loops.下面的这段代码试图减少循环次数。 If 'x' is extremely large, it helps to optimize the solution.
如果“x”非常大,则有助于优化解决方案。 The idea is to not do the divisibility check for each number starting from 1 to n-1.
这个想法是不要对从 1 到 n-1 的每个数字进行可分性检查。 Here, we use the fact that the least positive number divisible by 10 is 10. The next number of interest is 10 + 10 = 20, there by skipping numbers 11 to 19. This helps improve the performance.
在这里,我们使用可被 10 整除的最小正数是 10。下一个感兴趣的数是 10 + 10 = 20,因此跳过数字 11 到 19。这有助于提高性能。
x = input('Enter any number ')
y = 10
while y < x:
print(y)
y = y + 10
You can also try the following:您还可以尝试以下操作:
# grab the user's input
n = int(input('please enter a number: '))
# set x to 0, so the while loop can stop when 'n' is greater than '0'
x = 0
while n > x:
if n % 10 == 0:
print('{} is divisible by 10.'.format(n))
n -= 1
So basically the loop enters with the value that the user inputs, let's say 10
.所以基本上循环以用户输入的值进入,比方说
10
。
10
greater than 0
? 10
大于0
吗? Yes (while loop executes), the if
statement evaluates the remainder with the mod
.if
语句使用mod
计算余数。 The value is printed because the if statement evaluates True , the remainder is equal to zero.9
greater than 0
? 9
大于0
吗? Yes (while loop executes), the if
statement evaluates the remainder with the mod
.if
语句使用mod
计算余数。 The value is not printed because the if statement evaluates False , the remainder is not equal to zero.8
greater than 0
? 8
大于0
吗? Yes (while loop executes), the if
statement evaluates the remainder with the mod
.if
语句使用mod
计算余数。 The value is not printed because the if statement evaluates False , the remainder is not equal to zero. ... ...
And so on until n reaches 0
, the while loop stops because 0
is not greater than 0
.依此类推,直到n达到
0
,while 循环停止,因为0
不大于0
。
我认为使用内联 for 循环:
print([i for i in range(10,n,10) if i % 4 == 0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.