[英]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:另外,不要将数字转换为
float
, int
就足够了:
# 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
会给我们True
或False
。 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.