3 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊
范圍已經(jīng)定義,放一個(gè) count
count = 0
for i in range(1, 1000):
if i%2 != 0 and i%3 != 0:
count += 1
print("The number is {}".format(i))
print("Count: {}".format(count))
輸出:
The number is 1
The number is 5
The number is 7
The number is 11
The number is 13
.
.
.
The number is 991
The number is 995
The number is 997
Count: 333
編輯:
單線
print("Count: {}".format(sum(1 for i in range(1000) if i%2 != 0 and i%3 != 0)))

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊
count=0
for i in range(1, 1000):
if i%2 != 0 and i%3 != 0:
count=count+1
print(i)
只需在 IF 塊內(nèi)計(jì)數(shù)

TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊
有 1000/2 = 500 個(gè)可被 2 整除的數(shù)和 1000/3 = 333 個(gè)可被 3 整除的數(shù)。其中,6 的倍數(shù)出現(xiàn)兩次,其中 1000/6 = 165 個(gè)。
因此 1000 - (500 + 333 - 166) = 333。
最多十億,你將有 1,000,000,000,000,000,000 - (500,000,000,000,000,000 - 333,333,333,333,333,333 - 166,666,636 - 166,666,636 =33,36,36,33,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36,36
添加回答
舉報(bào)