○ 알고리즘, 자료구조/2021 알고리즘
1000이하, 2와 3의배수 총합 : 변수 위치 고려
0ver-grow
2021. 5. 27. 17:33
반응형
내가 푼 방식1
num = 1
two = 0
three = 0
while num < 1000 :
if num % 2 == 0 : # 2의 배수
two = two + num
elif num % 3 == 0 : # 3의 배수
three = three + num
num += 1
print (two + three)
내가 푼 방식2
num = 1
sum_num = 0
while num < 1000 :
if num % 2 == 0 :
sum_num += num
elif num % 3 == 0 :
sum_num += num
num += 1
print(sum_num)
모범 답안 : or를 쓰면 되는구나
i = 1
total = 0
while i < 1000:
if i % 2 == 0 or i % 3 == 0:
total += i
i += 1
print(total)
반응형