简体   繁体   English

打印可被 3 或 5 整除的数字的个数

[英]Print the count of numbers that are divisible either by 3 or 5

Given a list of n integers, count the number of integers in the list that are either a multiple of 3 or a multiple of 5. (All the numbers are guaranteed to be distinct).给定一个包含 n 个整数的列表,计算列表中有多少个整数是 3 的倍数或 5 的倍数。(所有数字都保证不同)。

Input Format:输入格式:

Single line of input contains a list of space separated integers单行输入包含空格分隔的整数列表

Output Format: Output 格式:

Print the count of numbers that are divisible either by 3 or 5打印可被 3 或 5 整除的数字的个数

Example:例子:

Input:输入:

1 3 5 6 7 9 11 13 15 18 20 21 1 3 5 6 7 9 11 13 15 18 20 21

Output: Output:

8 8个

My Code:我的代码:

x=input()
a=list(map(float, input().strip().split()))[:x]
c=0
for i in range(1,x+1):
  if ((i%3==0) & (i%5==0)):
    c=c+1
    print(c, end="")

output after running my code: output 运行我的代码后:

运行我的代码后的输出

Looking at your code, you don't have to use 2x input() (one time is sufficient).查看您的代码,您不必使用 2x input() (一次就足够了)。 Also, don't convert the numbers to float , the int is sufficient:另外,不要将数字转换为floatint就足够了:

# your input, you can substitute for `s = input()` later:
s = "1 3 5 6 7 9 11 13 15 18 20 21"

# convert numbers to integer:
numbers = [int(n) for n in s.split()]

print(sum(n % 3 == 0 or n % 5 == 0 for n in numbers))

Prints:印刷:

8

NOTE:笔记:

n % 3 == 0 or n % 5 == 0 will give us True or False . n % 3 == 0 or n % 5 == 0会给我们TrueFalse We can use sum() here to sum True values together ( True is equal to 1 ).我们可以在这里使用sum()True值加在一起( True等于1 )。

Check this out.看一下这个。

a=list(map(float, input().split()))
c=0
for i in a:
  if (i%3==0) or (i%5==0):
    c=c+1
print(c, end="")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 唯一允许的数字是可以被2或3整除的数字 - The only numbers allowed are those divisible by either 2 or 3 如何打印0到100之间可被3和5整除的数字? - How to print numbers from 0 to 100 that are divisible by 3 and also 5? 打印范围内可以被 4 或 5 整除的所有数字,但不能同时被 4 或 5 整除 - print all the numbers in a range that are divisible by 4 or 5, but not both 无法得到 n 以下所有数字的总和可被 3 或 5 整除 - Can't get the sum of all numbers below n divisible by either 3 or 5 打印从 1 到 100 的所有数字的列表,跳过可被 3 或 5 整除的数字 - print list of all numbers from 1 to 100, skip numbers divisible by 3 or 5 打印 1-100 之间的数字,跳过可被 3 和 5 整除的数字 - Print the numbers from 1-100 skipping the numbers divisible by 3 & 5 Python 3 - 从平方值可被 8 整除的列表中打印数字 - Python 3 - Print numbers from a list whose square value is evenly divisible by 8 Python:打印x和y可除范围内的所有数字 - Python: Print all numbers in range divisible by x and y 仅当立方体能被 4 整除时,打印数字 1 到 10 的立方体 - Print cubes of the numbers 1 through 10 only if the cube is evenly divisible by four 如何打印范围内的数字但排除给定可整除数字的数字 - How to print numbers from range but exclude a number by a given divisible number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM