[Swift] ๋ฐฑ์ค€2630 ์ƒ‰์ข…์ด ๋งŒ๋“ค๊ธฐ
ยท
๐Ÿ“ ์ฝ”ํ…Œ/BOJ
import Foundation let N = Int(readLine()!)! var board = [[Int]]() var counts = [0, 0] for _ in 0.. Bool { for i in x..
[Swift] ๋ฐฑ์ค€ 1541 ์žƒ์–ด๋ฒ„๋ฆฐ ๊ด„ํ˜ธ
ยท
๐Ÿ“ ์ฝ”ํ…Œ/BOJ
import Foundation /// ์ฒซ์งธ ์ค„์— ์‹์ด ์ฃผ์–ด์ง ( 0~9, +, -๋กœ๋งŒ ์ด๋ฃจ์–ด์ง) /// ์ฒ˜์Œ๊ณผ ๋งˆ์ง€๋ง‰๋ฌธ์ž๋Š” ์ˆซ์ž /// ์—ฐ์†ํ•ด์„œ 2๊ฐœ ์ด์ƒ์˜ ์—ฐ์‚ฐ์ž๊ฐ€ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š์Œ /// 5์ž๋ฆฌ๋ณด๋‹ค ๋งŽ์ด ์—ฐ์†๋˜๋Š” ์ˆซ์ž๋Š” ์—†์Œ /// ์ˆ˜๋Š” 0์œผ๋กœ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ์Œ /// ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๊ธธ์ด : 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์Œ let line = readLine()! let nums = line.components(separatedBy: ["+","-"]).map { Int($0)! } var opers = [Character]() var sum = 0 // ์—ฐ์‚ฐ์ž ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ for c in line { if c == "+" || c == "-" { opers.append(c) } } // ๊ด„ํ˜ธ์ ์šฉ : - ๋‹ค์Œ ..
[Swift] ๋ฐฑ์ค€ 1922 ์ฟผ๋“œํŠธ๋ฆฌ
ยท
๐Ÿ“ ์ฝ”ํ…Œ/BOJ
import Foundation /// ์ฃผ์–ด์ง„ ์˜์ƒ์ด ๋ชจ๋‘ 0์œผ๋กœ๋งŒ ๋˜์–ด์žˆ์œผ๋ฉด ์••์ถ• ๊ฒฐ๊ณผ๋Š” "0" /// ๋ชจ๋‘ 1๋กœ๋งŒ ๋˜์–ด์žˆ์œผ๋ฉด ์••์ถ•๊ฒฐ๊ณผ๋Š” "1" /// ๋งŒ์•ฝ 0๊ณผ 1์ด ์„ž์—ฌ์žˆ์œผ๋ฉด ์ „์ฒด๋ฅผ ํ•œ๋ฒˆ์— ๋‚˜ํƒ€๋‚ด์ง€ ๋ชปํ•˜๊ณ  /// ์™ผ์ชฝ ์œ„, ์˜ค๋ฅธ์ชฝ ์œ„, ์™ผ์ชฝ ์•„๋ž˜, ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ ์ด๋ ‡๊ฒŒ 4๊ฐœ์˜ ์˜์ƒ์œผ๋กœ ๋‚˜๋ˆ„์–ด ์••์ถ•ํ•˜๊ฒŒ๋œ๋‹ค. /// ์ด 4๊ฐœ์˜ ์˜์—ญ์„ ์••์ถ•ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๊ด„ํ˜ธ ์•ˆ์— ๋ฌถ์–ด์„œ ํ‘œํ˜„ํ•œ๋‹ค. /// ๋ถ„ํ• ์ •๋ณต์ด๋ž€? ๋ฌธ์ œ๋ฅผ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ ๋‚˜๋ˆ„์–ด์„œ ๋‹ค์‹œ ํ•ฉ๋ณ‘ํ•˜์—ฌ ๋ฌธ์ œ์˜ ๋‹ต์„ ์–ป๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ /// ์ƒ์œ„์˜ ํ•ด๋‹ต์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜๋กœ ๋‚ด๋ ค๊ฐ€๋ฉด์„œ ํ•˜์œ„์˜ ํ•ด๋‹ต์„ ๊ตฌํ•จ let N = Int(readLine()!)! var board = [[Int]]() for _ in 0.. Bool { for i in x..
[Swift] ๋ฐฑ์ค€ 1780 ์ข…์ด์˜ ๊ฐœ์ˆ˜
ยท
๐Ÿ“ ์ฝ”ํ…Œ/BOJ
์˜ค๋Š˜ ํ•˜๋ฃจ์ข…์ผ ํ’€๋ ค๊ณ  ๋„์ „ํ–ˆ๋Š”๋ฐ ์‹คํŒจํ•ด์„œ ๊ฒฐ๊ตญ ๊ตฌ๊ธ€๋ง์„ ํ–ˆ๋‹ค.. ์ด ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ N์˜ ์ตœ๋Œ€๊ฐ’์€ 3^7 = 2187์ด๋‹ค. ์ฆ‰ N^2 = 4782969 1์ดˆ์— 2000๋งŒํšŒ ์—ฐ์‚ฐ๋œ๋‹ค๊ณ  ํ•ด์„œ 2์ดˆ = 4์ฒœ๋งŒํšŒ์•ˆ์— ํ’€๋ฉด๋˜๋‹ˆ๊นŒ O(N^2)๋กœ ํ’€๋ฉด ๋  ์ค„ ์•Œ์•˜๋Š”๋ฐ .. ๊ณ„์† ์‹œ๊ฐ„์ดˆ๊ณผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ๊ตฌ๊ธ€๋ง ์ค‘ ํŒŒ์ด์ฌ์œผ๋กœ ํ‘ผ ์ฝ”๋“œ๋“ค์„ Swift์–ธ์–ด๋กœ ๋ฐ”๊ฟ” ํ’€์–ด๋ด๋„.. ํŒŒ์ด์ฌ์—์„œ๋Š” ํ’€๋ฆฌ์ง€๋งŒ Swift์—์„œ๋Š” ํ’€๋ฆฌ์ง€ ์•Š์•˜๋‹คใ… ใ…  O(N^2logN), O(NlogN) ๋“ฑ๋“ฑ ๋‹ค์–‘ํ•œ ์‹œ๊ฐ„๋ณต์žก๋„๋กœ ๊ตฌํ˜„ํ•ด๋ดค๋Š”๋ฐ ๋ชจ๋‘ ์‹คํŒจํ–ˆ์—ˆ๋‹ค. import Foundation let N = Int(readLine()!)! var matrix = [[Int]]() for _ in 0..
[Swift] ๋ฐฑ์ค€ 1676 ํŒฉํ† ๋ฆฌ์–ผ 0์˜ ๊ฐœ์ˆ˜
ยท
๐Ÿ“ ์ฝ”ํ…Œ/BOJ
import Foundation let n = Int(readLine()!)! print(n/5 + n/25 + n/125) 2 x 5 ๊ฐ€ ํฌํ•จ๋˜๋ฉด 0์˜ ๊ฐœ์ˆ˜๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚  ์ˆ˜๋ก 2๊ฐ€ 5๋ณด๋‹ค ๋งŽ์„ ๊ฑฐ๋‹ˆ๊นŒ 5์˜ ๊ฐœ์ˆ˜๋งŒ ์•Œ๋ฉด 0์˜ ๊ฐœ์ˆ˜๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 500๋ฏธ๋งŒ์˜ ์ˆ˜ ์ค‘์—์„œ๋Š” 5, 5^2, 5^3 3๊ฐ€์ง€ ๊ฒฝ์šฐ ๋ฐ–์— ์—†๊ธฐ๋•Œ๋ฌธ์— 3๊ฐœ์˜ ์ผ€์ด์Šค๋งŒ ๋‚˜๋ˆ ์„œ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
[Swift] ๋ฐฑ์ค€1012 ์œ ๊ธฐ๋†๋ฐฐ์ถ”
ยท
๐Ÿ“ ์ฝ”ํ…Œ/BOJ
import Foundation let T = Int(readLine()!)! for _ in 1...T { let MNK = readLine()!.components(separatedBy: " ").map { Int($0)! } let M = MNK[0] // ๋ฐฐ์ถ”๋ฐญ ๊ฐ€๋กœ๊ธธ์ด let N = MNK[1] // ๋ฐฐ์ถ”๋ฐญ ์ƒˆ๋กœ๊ธธ์ด let K = MNK[2] // ๋ฐฐ์ถ”๊ฐ€ ์‹ฌ์–ด์ ธ ์žˆ๋Š” ์œ„์น˜์˜ ๊ฐœ์ˆ˜ // M * N 2์ฐจ์› ๋ฐฐ์—ด ์ƒ์„ฑ var graph = [[Int]](repeating: [Int](repeating: 0, count: M), count: N) for _ in 1...K { let XY = readLine()!.components(separatedBy: " ").map { Int($0)! } let ..
[Swift] ๋ฐฑ์ค€1260 DFS์™€ BFS
ยท
๐Ÿ“ ์ฝ”ํ…Œ/BOJ
import Foundation let NMV = readLine()!.components(separatedBy: " ").map { Int($0)! } let N = NMV[0] let M = NMV[1] let V = NMV[2] var graph = [String: [String]]() initializeGraph() for _ in 1...M { let AB = readLine()!.components(separatedBy: " ") let A = AB[0] let B = AB[1] graph[A]?.append(B) graph[B]?.append(A) } printArray(DFS(start: String(V))) printArray(BFS(start: String(V))) func DFS(st..
[Swift] ๋ฐฑ์ค€1389 ์ผ€๋นˆ ๋ฒ ์ด์ปจ์˜ 6๋‹จ๊ณ„ ๋ฒ•์น™
ยท
๐Ÿ“ ์ฝ”ํ…Œ/BOJ
import Foundation /// ์ผ€๋นˆ ๋ฒ ์ด์ปจ ์ˆ˜ : ๋ชจ๋“  ์‚ฌ๋žŒ๊ณผ ์ผ€๋นˆ ๋ฒ ์ด์ปจ ๊ฒŒ์ž„์„ ํ–ˆ์„ ๋•Œ ๋‚˜์˜ค๋Š” ๋‹จ๊ณ„์˜ ํ•ฉ /// BOJ ์œ ์ €๊ฐ€ 5๋ช…์ด๊ณ , 1-3, 1-4, 2-3, 3-4, 4-5๊ฐ€ ์นœ๊ตฌ์ธ ๊ฒฝ์šฐ /// 1์€ ์นœ๊ตฌ์™€ ์ด์–ด์ง€๊ธฐ ์œ„ํ•ด 1-3-2, 1-3, 1-4, 1-4-5 ์ด๋Ÿฐ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์นœ๋‹ค. ์ฆ‰ ์ผ€๋นˆ ๋ฒ ์ด์ปจ ์ˆ˜๋Š” 2 + 1 + 1 + 2 = 6์ด๋‹ค. /// BOJ ์œ ์ €์˜ ์ˆ˜์™€ ์นœ๊ตฌ๊ด€๊ณ„๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ผ€๋นˆ ๋ฒ ์ด์ปจ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์‚ฌ๋žŒ์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. /// N : ์œ ์ €์˜ ์ˆ˜ /// M : ์นœ๊ตฌ๊ด€๊ณ„์˜ ์ˆ˜ let NM = readLine()!.components(separatedBy: " ").map { Int($0)! } let N = NM[0] let M = NM[1]..
[Swift] ๋ฐฑ์ค€ 11650 ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ
ยท
๐Ÿ“ ์ฝ”ํ…Œ/BOJ
import Foundation let N = Int(readLine()!)! var tuples = [(x: Int, y: Int)]() for _ in 1...N { let xy = readLine()!.components(separatedBy: " ").map { Int($0)! } let x = xy[0] let y = xy[1] tuples.append((x, y)) } tuples = tuples.sorted(by: { $0.y < $1.y }).sorted(by: { $0.x < $1.x }) for tuple in tuples { print(tuple.x, tuple.y) } ํŠœํ”Œ๋กœ ํ’€์—ˆ๋‹ค. ์ •๋ ฌํ• ๋•Œ๋Š” ์ •๋ ฌํ•˜๋Š” ์ˆœ์„œ๋ฅผ ์œ ์˜ํ•˜๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.
[Swift] ๋ฐฑ์ค€ 10866 ๋ฑ
ยท
๐Ÿ“ ์ฝ”ํ…Œ/BOJ
import Foundation let N = Int(readLine()!)! var deque = [Int]() for _ in 1...N { let line = readLine()! printNum(command: line) } func printNum(command: String) { let arr = command.components(separatedBy: " ") switch arr[0] { case "push_back": return deque.append(Int(arr[1])!) case "push_front": if deque.isEmpty { deque.append(Int(arr[1])!) } else { deque.insert(Int(arr[1])!, at: 0) } return () ..
JerryiOS
'๐Ÿ“ ์ฝ”ํ…Œ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (4 Page)