Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 2089๋ฒˆ - -2์ง„๋ฒ•

๊ฐ์ž ๐Ÿฅ” 2022. 2. 26. 16:46
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/2089

 

๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด - ๋งž์•˜์Šต๋‹ˆ๋‹ค!

๋ณต์žกํ•˜๊ฒŒ ์ƒ๊ฐํ•˜์ง€ ์•Š๊ณ  if๋ฌธ์œผ๋กœ ์ƒํ™ฉ์„ ๋‚˜๋ˆ„์–ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค. ์กฐ๊ธˆ ๋” ํšจ์œจ์ ์ธ ์ฝ”๋”ฉ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ด์•ผ๊ฒ ๋‹ค.

var num = Int(readLine()!)!
var ans = ""

while true {
    if num == -1 {
        ans += "11"
        break
    } else if num == 0 {
        ans += "0"
        break
    } else if num == 1{
        ans += "1"
        break
    }
    
    if num < 0 {
        if num % 2 != 0 {
            num += -1
            num /= -2
            ans += "1"
        } else {
            num /= -2
            ans += "0"
        }
    }
    else if num > 0 {
        if num % 2 != 0 {
            num -= 1
            num /= -2
            ans += "1"
        } else {
            num /= -2
            ans += "0"
        }
    }
}

print(String(ans.reversed()))
๋ฐ˜์‘ํ˜•