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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 1212๋ฒˆ - 8์ง„์ˆ˜ 2์ง„์ˆ˜

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

๋ฌธ์ œ๋งํฌ

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

 

1212๋ฒˆ: 8์ง„์ˆ˜ 2์ง„์ˆ˜

์ฒซ์งธ ์ค„์— 8์ง„์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ˆ˜์˜ ๊ธธ์ด๋Š” 333,334์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

www.acmicpc.net

 

๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด - ์‹œ๊ฐ„์ดˆ๊ณผ (์–ด๋””์„œ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ค„์—ฌ์•ผํ• ๊นŒ์š”?) ํ•ด๊ฒฐ ์™„๋ฃŒ 

  • input์œผ๋กœ ์ˆซ์ž ํ•˜๋‚˜ํ•˜๋‚˜์”ฉ ๋Š์–ด์„œ ๋ฐฐ์—ด๋กœ ์ €์žฅ
  • ์ˆซ์ž ํ•˜๋‚˜์”ฉ 2์ง„์ˆ˜๋กœ ๊ตฌํ•˜๊ณ , reverse ํ•ด์ค„๊ฑฐ์ž„
    • 314 ์ด๋ฉด, 11 / 001 / 100 ์ด ๋˜์–ด์•ผํ•จ
    • 1์„ 2์ง„์ˆ˜๋กœ ๊ตฌํ•˜๋ฉด 1์ž„. --> ์ด๋†ˆ์„ ์„ธ์ž๋ฆฌ์ˆ˜๋กœ ๋ชจ์กฐ๋ฆฌ ์ฑ„์›Œ์ฃผ๊ธฐ ์œ„ํ•ด ์•ž์— 0 ๋‘๊ฐœ๋ฅผ ๋„ฃ์–ด์ฃผ์–ด์•ผํ•จ.
    • ์ด๋Ÿฐ ๊ณผ์ •์ด ํ•„์š”ํ•˜๋ฏ€๋กœ, if ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ 0์„ ๋„ฃ์–ด์คŒ 
    • ๋Œ€์‹ , 3์ฒ˜๋Ÿผ ๋งจ์•ž์—์žˆ๋Š” ์ˆซ์ž์ธ ๊ฒฝ์šฐ์—๋Š” 011 ๋กœ ๋ฐ”๊ฟ”์ค„ ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ๊ทธ๋ƒฅ 11๋กœ ๋‘๊ธฐ ์œ„ํ•ด์„œ i๊ฐ€ 0 ์ด์ƒ์ผ ๊ฒฝ์šฐ์—๋งŒ for๋ฌธ์„ ํ†ต๊ณผ์‹œํ‚ด

์‹œ๊ฐ„์ดˆ๊ณผ... ์Šค์œ„ํ”„ํŠธ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ ํ’€์ด๋ฅผ ๋ณผ ์ˆ˜๊ฐ€ ์—†๊ณ , ๊ตฌ๊ธ€์— ๋งŽ์€ ๋ฆฌ๋ทฐ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋„์›€์„ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค ใ… ใ…  ์Šค์œ„ํ”„ํŠธ ๊ณ ์ˆ˜๋‹˜๋“ค ์•Œ๋ ค์ฃผ์„ธ์š”.. ์™œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋œจ๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ์–ด๋Š๋ถ€๋ถ„์—์„œ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ค„์—ฌ์•ผํ•˜๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.

var input = Array(readLine()!)

for i in 0..<input.count {
    var num = Int(String(input[i]))!
    var answer = ""

    while num != 1 {
        answer += String(num%2)
        num = num/2
    }
    answer += "1"

    if i>0 && answer.count == 1 {
        answer += "00"
    } else if i>0 && answer.count == 2{
        answer += "0"
    }
    print(String(answer.reversed()), terminator: "")
}

 

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

ํ‰ํ™”๋กœ์šด ์–ด๋Š๋‚  ์Šค์œ„ํ”„ํŠธ ์Šคํ„ฐ๋””์›์—๊ฒŒ ์นดํ†ก์ด์™”๋‹ค! 

๋Œ€๋ฐ•... ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋œฌ ์ด์œ ๊ฐ€ ๋ฌดํ•œ๋ฃจํ”„์— ๋น ์ง€๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. ใ…‹ใ…‹ใ…‹ ์ด๊ฑธ ๋ชฐ๋ž๋‹ค๋‹ˆ... ๋‚ด ์ฝ”๋“œ๋ฅผ ์ •์„ฑ์Šค๋Ÿฝ๊ฒŒ ์ฝ์–ด๋ด์ฃผ์‹œ๊ณ  ํ•ด๊ฒฐ๋ฐฉ๋ฒ•๊นŒ์ง€ ์ œ์‹œํ•ด์ฃผ์‹  ์ œ ์Šคํ„ฐ๋””์›๋‹˜ ๋„˜ ์Šค์œ— ๊ทธ๋ฆฌ๊ณ  ๋„˜ ๊ฐ๋™์ด์—ˆ๋‹ค. ๋•๋ถ„์— ์ž˜ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค~~!~!~!~!

์‹ค์ œ๋กœ "0"์ด ์ž…๋ ฅ๋˜๋ฉด ์ถœ๋ ฅ์ด ์•ˆ๋˜๋Š” ๋ฌดํ•œ๋ฃจํ”„ ์ƒํƒœ์˜€๊ณ , ์ด๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ if๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋˜, ์Šค์œ„ํ”„ํŠธ ์–ธ์–ด ํŠน์„ฑ์ƒ ์•„์ง ์ž…์ง€๊ฐ€ ํƒ€ ์–ธ์–ด๋งŒํผ ๋„“์ง€ ์•Š์•„ ์ •๋‹ต ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ์ฐพ๊ธฐ ํž˜๋“ค์—ˆ๋Š”๋ฐ ๋ณธ์ธ์˜ ์ฝ”๋“œ๋„ ๋ณด๋‚ด์ฃผ์…”์„œ ์ฐธ๊ณ ํ•ด๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค. case ๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ‘ผ ์ ์ด ๋…ํŠนํ–ˆ๊ณ , ๋‹จ์ˆœํ•˜๋ฉด์„œ๋„ ํšจ์œจ์ ์ด๋ผ๊ณ  ๋Š๊ปด์กŒ๋‹ค.

var input = Array(readLine()!)

for i in 0..<input.count {
    var num = Int(String(input[i]))!
    var answer = ""

    if num == 0 {
        answer += "0"
    }else {
        while num != 1 {
            answer += String(num%2)
            num = num/2
        }
        answer += "1"
    }

    if i>0 && answer.count == 1 {
        answer += "00"
    } else if i>0 && answer.count == 2{
        answer += "0"
    }
    print(String(answer.reversed()), terminator: "")
}
๋ฐ˜์‘ํ˜•