From 686fb84ef832b497e0cf94a5030046bb4cd0a136 Mon Sep 17 00:00:00 2001 From: songyc macbook Date: Fri, 2 Jan 2026 21:41:59 +0900 Subject: [PATCH] baekjoon 20260102 --- code_study/Baekjoon/swift/10156.swift | 10 +++++ code_study/Baekjoon/swift/9466.swift | 55 +++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 code_study/Baekjoon/swift/10156.swift create mode 100644 code_study/Baekjoon/swift/9466.swift diff --git a/code_study/Baekjoon/swift/10156.swift b/code_study/Baekjoon/swift/10156.swift new file mode 100644 index 0000000..6a15591 --- /dev/null +++ b/code_study/Baekjoon/swift/10156.swift @@ -0,0 +1,10 @@ +func sol() -> Int { + guard let input = readLine() else { return 0 } + let info = input.split(separator: " ").compactMap{Int($0)} + + let ans = { (a: Int, b: Int) -> Int in return a - b > 0 ? a - b : 0 } + + return ans(info[0]*info[1], info[2]) +} + +print(sol()) \ No newline at end of file diff --git a/code_study/Baekjoon/swift/9466.swift b/code_study/Baekjoon/swift/9466.swift new file mode 100644 index 0000000..d89297c --- /dev/null +++ b/code_study/Baekjoon/swift/9466.swift @@ -0,0 +1,55 @@ +func solve() -> Int { + guard let n = Int(readLine() ?? ""), + let input = readLine() + else { return 0 } + + let choice: [Int] = [0] + input.split(separator: " ").compactMap({Int($0)}) + var visited: [Bool] = Array(repeating: false, count: n+1) + var done: [Bool] = Array(repeating: false, count: n+1) + var cnt: Int = 0 + + for i in 1...n { + if i == choice[i] { + cnt += 1 + visited[i] = true + done[i] = true + } + } + + func dfs(_ current: Int) { + visited[current] = true + let next = choice[current] + + if !visited[next] { + dfs(next) + } + else if !done[next] { + cnt += 1 + var n = next + while n != current { + cnt += 1 + n = choice[n] + } + } + + done[current] = true + } + + for i in 1...n { + if !done[i] { + dfs(i) + } + } + + return n-cnt +} + +var ans: [Int] = [] + +if let T = Int(readLine() ?? "") { + for _ in 0..