简体   繁体   English

找出所有能被 10 整除且小于 n 的正数

[英]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

  • Is 10 greater than 0 ? 10大于0吗? Yes (while loop executes), the if statement evaluates the remainder with the mod .是的(while 循环执行), if语句使用mod计算余数。 The value is printed because the if statement evaluates True , the remainder is equal to zero.打印该值因为 if 语句计算True ,余数等于0。 At last, n is subtracted by 1最后,n减1
  • Is 9 greater than 0 ? 9大于0吗? Yes (while loop executes), the if statement evaluates the remainder with the mod .是的(while 循环执行), if语句使用mod计算余数。 The value is not printed because the if statement evaluates False , the remainder is not equal to zero.该值打印,因为 if 语句计算False ,余数等于零。 At last, n is subtracted by 1最后,n减1
  • Is 8 greater than 0 ? 8大于0吗? Yes (while loop executes), the if statement evaluates the remainder with the mod .是的(while 循环执行), if语句使用mod计算余数。 The value is not printed because the if statement evaluates False , the remainder is not equal to zero.该值打印,因为 if 语句计算False ,余数等于零。 At last, n is subtracted by 1最后,n减1

... ...

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.

相关问题 打印所有小于 N 的完美数 - Print all perfect numbers less than N 接受一个正的 integer n 作为输入,并找到打印最小的 integer 可以被范围 [1, n] 内的所有整数整除 - Accept a positive integer n as input and find the print the smallest integer that is divisible by all the integers in the range [1, n] 找出小于n的最大素数,其中n =〜10 ^ 230 - Find the greatest prime less than n, with n = ~10^230 你如何得到所有能被 10 整除且总和为 100 的数字的所有组合? - How do you get all the combinations of n numbers divisible by 10 and whose sum is 100? 能被 1 到 20 的所有数整除的最小正数? - Smallest positive number that is evenly divisible by all of the numbers from 1 to 20? 有效地生成小于N的所有复合数(用它们的因子分解) - Efficiently generate all composite numbers less than N (with their factorizations) 如何测试一个数字是否可以被小于1的十进制整除? (54.10%.10) - how to test if a number is divisible by a decimal less than 1? (54.10 % .10) 字典理解以找到所有可以被 1 以外的一位数整除的 b/w 1 和 25 数字 (2-9) - Dictionary comprehension to find all numbers b/w 1 and 25 that are divisible by a single digit other than 1 (2-9) 查找长度小于或等于 L 的 n 的所有分区 - Find all partitions of n of length less-than-or-equal to L 找出所有小于 200 万的奇数斐波那契数的总和 - Find the sum of all odd Fibonacci numbers less than 2 million
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM