简体   繁体   中英

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).

Input Format:

Single line of input contains a list of space separated integers

Output Format:

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

Example:

Input:

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

Output:

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:

运行我的代码后的输出

Looking at your code, you don't have to use 2x input() (one time is sufficient). Also, don't convert the numbers to float , the int is sufficient:

# 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 . We can use sum() here to sum True values together ( True is equal to 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="")

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM