簡體   English   中英

打印可被 3 或 5 整除的數字的個數

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

給定一個包含 n 個整數的列表,計算列表中有多少個整數是 3 的倍數或 5 的倍數。(所有數字都保證不同)。

輸入格式:

單行輸入包含空格分隔的整數列表

Output 格式:

打印可被 3 或 5 整除的數字的個數

例子:

輸入:

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

Output:

8個

我的代碼:

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 運行我的代碼后:

運行我的代碼后的輸出

查看您的代碼,您不必使用 2x input() (一次就足夠了)。 另外,不要將數字轉換為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))

印刷:

8

筆記:

n % 3 == 0 or n % 5 == 0會給我們TrueFalse 我們可以在這里使用sum()True值加在一起( True等於1 )。

看一下這個。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM