Bibi's DevLog ๐Ÿค“๐ŸŽ

[๋ฐฑ์ค€ 4504] ๋ฐฐ์ˆ˜ ์ฐพ๊ธฐ (Swift) ๋ณธ๋ฌธ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด Swift

[๋ฐฑ์ค€ 4504] ๋ฐฐ์ˆ˜ ์ฐพ๊ธฐ (Swift)

๋น„๋น„ bibi 2022. 10. 21. 11:12

4504 ๋ฐฐ์ˆ˜ ์ฐพ๊ธฐ

4504๋ฒˆ: ๋ฐฐ์ˆ˜ ์ฐพ๊ธฐ

  • ์–ด๋–ค ์ˆซ์ž๊ฐ€ ์ฃผ์–ด์ง„ ์ •์ˆ˜์˜ ๋ฐฐ์ˆ˜์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋‹จํ•ด ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ.
  • ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€์ง€ ์•Š๋Š” ๋Œ€์‹ , ์ž…๋ ฅ์˜ ๋์— 0์ด ์ฃผ์–ด์ง„๋‹ค.

ํ’€์ด1

let multiple = Int(readLine()!)!
while true {
    let input = Int(readLine()!)!
    if input == 0 {
        break
    }
    if input % multiple == 0 {
        print("\(input) is a multiple of \(multiple).")
    } else {
        print("\(input) is NOT a multiple of \(multiple).")
    }
}
  • ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž์˜ ๊ฒฐ๊ณผ๊ฐ€ 0์ด๋ฉด ๋ฐฐ์ˆ˜๊ฐ€ ๋œ๋‹ค.

ํ’€์ด2

let multiple = Int(readLine()!)!
while let input = Int(readLine()!), input != 0 {
    if input % multiple == 0 {
        print("\(input) is a multiple of \(multiple).")
    } else {
        print("\(input) is NOT a multiple of \(multiple).")
    }
}

์ œ์ถœ ํ›„์— ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค ํ’€์ด๋ฅผ ๋ณด๋‹ˆ while true ๋ถ€๋ถ„๊ณผ ํƒˆ์ถœ ๊ตฌ๋ฌธ์„ ์—†์• ๊ณ , while let์„ ํฌํ•จํ•œ ์กฐ๊ฑด์ ˆ๋กœ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

while let ์‚ฌ์šฉ์— ์ต์ˆ™ํ•ด์งˆ ์ˆ˜ ์žˆ๋„๋ก ๋” ์ž์ฃผ ์จ๋ด์•ผ๊ฒ ๋‹ค..!

*while let์œผ๋กœ input์„ ๋ฐ›์œผ๋ฉด, input์ด ์˜ต์…”๋„์ด์–ด์•ผ ํ•˜๋ฏ€๋กœ ๊ฐ•์ œ์–ธ๋ž˜ํ•‘ ํ•˜๋‚˜๋ฅผ ์ง€์›Œ์ค˜์•ผ ํ•จ์— ์œ ์˜ํ•จ