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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 11655๋ฒˆ - ROT13

๊ฐ์ž ๐Ÿฅ” 2022. 2. 17. 19:08
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๋งํฌ 

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

 

11655๋ฒˆ: ROT13

์ฒซ์งธ ์ค„์— ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž, ์†Œ๋ฌธ์ž, ๊ณต๋ฐฑ, ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. S์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

www.acmicpc.net

 

๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด (1) - ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ

  • ๋ฐฐ์—ด ๋‘๊ฐœ๋ฅผ ์‚ฌ์šฉํ•ด์„œ 26๋ฌธ์ž 26๋ฌธ์ž๋ฅผ ๋„ฃ์„๊นŒ ํ•˜๋‹ค๊ฐ€ dic์ด ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋” ์ ์€๊ฒƒ(?)์„ ์•Œ๊ณ  dic์œผ๋กœ ๊ตฌ์„ฑํ–ˆ๋‹ค.
  • ์•„๋ฌด๋ž˜๋„ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ dic์— ๋„ฃ๋Š” ๊ฒƒ์ด๋‹ค ๋ณด๋‹ˆ, ๋Ÿฐํƒ€์ž„์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
let line = Array(readLine()!)
var answer: [Character] = []
let dic: [String:Character] = ["a":"n", "b":"o", "c":"p", "d":"q", "e":"r", "f":"s", "g":"t", "h":"u",
                                  "i":"v", "j":"w", "k":"x", "l":"y", "m":"z", "n":"a","o":"b", "p":"c", "q":"d",
                                  "r":"e", "s":"f", "t":"g", "u":"h", "v":"i", "w":"j", "x":"k", "y":"l", "z":"m",]

for l in line {
    if l.asciiValue! >= 65 && l.asciiValue! <= 90 {
        answer.append(Character(dic[String(l.lowercased())]!.uppercased()))
    } else {
        answer.append(dic[String(l)]!)
    }
}

print(answer.map { "\($0)"}.joined(separator: ""))

 

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

  • ๊ทธ๋ƒฅ ์ž…๋ ฅ๋ฐ›์€ line ์„ ํ•œ๊ธ€์ž์”ฉ ascii ๊ฐ’์œผ๋กœ ๋ฐ”๊พธ์–ด์„œ ํ’€์—ˆ๋‹ค.
  • ์Šค์œ„ํ”„ํŠธ๋ง๊ณ  ํŒŒ์ด์ฌ์œผ๋กœ ํ‘ผ ํ’€์ด๋ฅผ ์ฐพ์•„๋ณด์•˜๋Š”๋ฐ, ๊ฑฐ๊ธฐ์„œ ํ•ด๋‹ต(ํžŒํŠธ)๋ฅผ ์–ป์—ˆ๋‹ค.
  • ์ด๋ ‡๊ฒŒ dic๊ณผ ๋ฐฐ์—ด์— ๋งŽ์€ ๋ฌธ์ž๋ฅผ ๋„ฃ์–ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š”, ascii๋ฅผ ์ด์šฉํ•ด์•ผ๊ฒ ๋‹ค.
let line = readLine()!
var answer = ""

for ch in line {
    var rot13 = ch.asciiValue!
    
    if rot13 >= 65 && rot13 <= 90 {
        rot13 += 13
        if rot13 > 90 {
            rot13 -= 26
        }
    } else if rot13 >= 97 && rot13 <= 122 {
        rot13 += 13
        if rot13 > 122 {
            rot13 -= 26
        }
    }
    answer += "\(UnicodeScalar(rot13))"
}

print(answer)

 

์•„ ๊ทผ๋ฐ, ์‹œ๊ฐ„๋ณต์žก๋„์— ๋Œ€ํ•œ ๊ฐœ๋…์€ ์•Œ์ง€๋งŒ, ์ฝ”๋“œ๋ณ„๋กœ ์–ด๋–ป๊ฒŒ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ผ๊นŒ? ๊ธฐ๋ณธ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜/์ž๋ฃŒ๊ตฌ์กฐ ์ˆ˜์—…์„ ๋“ฃ์ง€ ์•Š์•„์„œ ๋ชจ๋ฅด๊ฒ ๋‹ค. ๋ฌด์ง€ํ•œ ๋ถ€๋ถ„์ด ์ฐธ๋งŽ๋‹ค ใ… ใ…   ์ด๊ฑฐ ์•Œ์•„๋ณด์ž. 

๋ฐ˜์‘ํ˜•