본문 바로가기

 𝗔𝗣𝗣𝗟𝗘/ALGORITHM

[내일배움캠프] 데일리 루틴(iOS_3회차) - 콜라 문제

이미지를 클릭하면 코딩테스트 페이지로 이동합니다

 

 

내 풀이


  
import Foundation
func solution(_ a:Int, _ b:Int, _ n:Int) -> Int {
var coke = 0
var emptyBottle = n
var result = 0
while emptyBottle >= a {
var num = emptyBottle / a * b
result = result + (emptyBottle / a * b)
emptyBottle = emptyBottle % a
coke += num
if coke > 0 {
emptyBottle += coke
coke = 0
}
}
return result
}

 

- coke, emptyBottle, result 변수 초기화

- emptyBottlea보다 크거나 같은 동안 반복

- 각 반복마다 emptyBottlea로 나눈 몫에 b를 곱한 값을 result에 더하기

- emptyBottlea로 나눈 나머지로 갱신

- coke 변수를 사용하여 마신 음료의 양을 더한 후, emptyBottle에 더하기

- 만약 coke가 0보다 크다면, emptyBottlecoke를 더하고 coke를 0으로 초기화

- 반복이 종료되면 최종적으로 result 값 반환

 

 

 

 

 

 

 

 

 

while이란?

 

Swift 기초 문법 - 반복문

반복문 💡 반복문 For - in → 배열, 딕셔너리 등을 순회하거나 특정 횟수만큼 로직을 반복할 때 주로 사용 for 각 value의 변수 이름 in 순회할 수 있는 타입 { // 내부 로직 } let alphabets: [String] = ["a", "

wood-fxrest.tistory.com

 

 

 

Recent Posts
Visits
Today
Yesterday
Archives
Calendar
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31