๋๋ฆ๋๋ก ํ์๋๋ฐ, ํ๋ ธ๊ฑฐ๋ ์๊ฐ ์ด๊ณผ์ธ ์ฝ๋ ๋ชจ์์ง
BOJ: #2870 - ์ํ์์
์
๋ ฅ์ด ๋ค์ด์ค๋ ๋ฌธ์์ด์์ ์ซ์๋ ๋ฐฐ์ด์ ๋ฐ๋ก ์ ์ฅํด์ ์ฒ๋ฆฌํ๋ค. ๋ชจ๋ ๊ฐ์ด 0์ธ ๊ฒฝ์ฐ์๋ allSatisfy()
๋ฅผ ์ด์ฉํด์ ์ฒ๋ฆฌํ๊ณ , ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ์๋ ๋ชจ๋ 0์ ์ ๊ท์์ผ๋ก ์ฒ๋ฆฌํ๋ค. ๊ทธ๋ฆฌ๊ณ ์ ๋ ฌ์ swift string number sort
๋ก ๊ตฌ๊ธ๋งํด์ ๋์จ ๊ฑธ ์ฌ์ฉํ๋ค. ๊ทผ๋ฐ ํ๋ ธ์… ์ ํ๋ ธ๋์ง ๋ชจ๋ฅด๊ฒ ๋น
๋ฌธ์
https://www.acmicpc.net/problem/2870
๋ด๊ฐ ์์ฑํ ์ฝ๋
Swift
import Foundation
let n = Int(readLine()!)!
var nums = [String]()
for _ in 0..<n {
let input = readLine()!
var num = [Character]()
for char in input {
if char.isNumber {
num.append(char)
} else {
if !num.isEmpty {
if num.allSatisfy({$0 == "0"}) {
nums.append("0")
} else {
let trimmed = String(num).replacingOccurrences(of: "^0+", with: "", options: .regularExpression)
nums.append(trimmed)
num = []
}
}
}
}
if !num.isEmpty {
if num.allSatisfy({$0 == "0"}) {
nums.append("0")
} else {
let trimmed = String(num).replacingOccurrences(of: "^0+", with: "", options: .regularExpression)
nums.append(trimmed)
num = []
}
}
}
// ์ ๋ ฌ ๋ถ๋ถ ๊ณ ์น๊ธฐ
nums.sort(by: { $0.localizedStandardCompare($1) == .orderedAscending})
//nums.sort(by: { $0.compare($1, options: .numeric) == .orderedAscending})
for num in nums {
print(num)
}
BOJ: #2852 - NBA ๋๊ตฌ
๋ํ์์ ์ถ์ ๋๋ ๋ฌธ์ ๋ผ ๋ํ ํํ์ด์ง์์ ํ ์คํธ ์ผ์ด์ค๋ฅผ ๋ดค๋ค. ์ค๊ฐ์ ๋ฌธ์ ์ ๋ํ ์ดํด๋ฅผ ์๋ชปํ๋ค๋ ๊ฑธ ๋๊ผ๋๋ฐ ์ด๋ป๊ฒ ๊ณ ์ณ์ผํ ์ง ๋ชจ๋ฅด๊ฒ ์ด์ ๋ณด๋ฅ.
๋ฌธ์
https://www.acmicpc.net/problem/2852
๋ด๊ฐ ์์ฑํ ์ฝ๋
Swift
import Foundation
let n = Int(readLine()!)!
var info = [[String]]()
for _ in 0..<n {
let victory = readLine()!.split(separator: " ").map { String($0) }
info.append(victory)
}
var prevWin = info[0][0]
var prevTime = info[0][1].split(separator: ":").map { Int(String($0))! }
var firstTeam = [0, 0]
var secondTeam = [0, 0]
for i in 1..<n {
if prevWin != info[i][0] { // ์ด๊ธฐ๊ณ ์๋ ์๊ฐ
let time = info[i][1].split(separator: ":").map { Int(String($0))! }
var minute = time[0] - prevTime[0]
var second = time[1] - prevTime[1]
if second < 0 {
minute -= 1
second += 60
}
if prevWin == "1" { // 1๋ฒ ํ์ด ์ด๊ธฐ๊ณ ์๋ ์๊ฐ
firstTeam[0] += minute
firstTeam[1] += second
} else { // 2๋ฒ ํ์ด ์ด๊ธฐ๊ณ ์๋ ์๊ฐ
secondTeam[0] += minute
secondTeam[1] += second
}
prevWin = info[i][0]
prevTime = time
}
}
if prevWin == info[n-1][0] { // ๋ง์ง๋ง๊น์ง ์ด๊ธฐ๊ณ ์์์ผ๋ฉด
let time = info[n-1][1].split(separator: ":").map { Int(String($0))! }
var overMinute = 48 - time[0]
var overSecond = -time[1]
if overSecond < 0 {
overMinute -= 1
overSecond += 60
}
if prevWin == "1" { // 1๋ฒ ํ์ด ์ด๊ธฐ๊ณ ์๋ ์๊ฐ
firstTeam[0] += overMinute
firstTeam[1] += overSecond
} else { // 2๋ฒ ํ์ด ์ด๊ธฐ๊ณ ์๋ ์๊ฐ
secondTeam[0] += overMinute
secondTeam[1] += overSecond
}
}
print(String(format: "%02d", firstTeam[0]) + ":" + String(format: "%02d", firstTeam[1]))
print(String(format: "%02d", secondTeam[0]) + ":" + String(format: "%02d", secondTeam[1]))
BOJ: #1436 - ์ํ๊ฐ๋ ์
๋จ์ํ๊ฒ ์๊ฐํด์ 666๋ถํฐ n
๊ฐ์ 666์ด ๋ค์ด๊ฐ๋ ์ซ์ ๋ฐฐ์ด๊น์ง ์ํํด์ ์ฝ์
ํ๋๋ฐ, ์๊ฐ ์ด๊ณผ. ํ๊ธด, Xcode์์ 9999๋ฅผ ํ ๋ ๋๋ฆฌ๊ธด ํ๋ค. ์๊ฐ์ ๋ ์ด๋ป๊ฒ ์ค์ฌ์ผํ ์ง ๋ชจ๋ฅด๊ฒ ์ด์ ํจ์ค.
๋ฌธ์
https://www.acmicpc.net/problem/1436
๋ด๊ฐ ์์ฑํ ์ฝ๋
Swift
import Foundation
let n = Int(readLine()!)!
var nums = [String]()
var i = 666
while nums.count != n {
if String(i).contains("666") {
nums.append(String(i))
}
i += 1
}
print(nums[n-1])
BOJ: #14620 - ๊ฝ๊ธธ
ํ์ ๋ฌธ์ ์ฒ๋ผ ํ์๋๋ฐ, ์ด๋๊ฐ ์๋ชป๋ ๊ฑฐ์ผ…? ๋ ๋ชจ๋ฅด๊ฒ ๋ค /_\
๋ฌธ์
https://www.acmicpc.net/problem/14620
๋ด๊ฐ ์์ฑํ ์ฝ๋
Swift
let n = Int(readLine()!)!
var price = [[Int]]()
for _ in 0..<n {
let line = readLine()!.split(separator: " ").map { Int(String($0))! }
price.append(line)
}
var seeds = 3
var board = Array(repeating: Array(repeating: 0, count: n), count: n)
var ans = 0
while seeds != 0 {
var dict = [[Int]:Int]()
for i in 0..<n {
for j in 0..<n {
// ๊ฝ์ด ์จ์ ํ ํผ์ด์ผํจ
if j - 1 < 0 || j + 1 >= n || i - 1 < 0 || i + 1 >= n {
continue
}
// ์ด๋ฏธ ๊ฝ์ ์ฌ์ ๊ณณ์ ์๋จ
if board[i][j] == 1 || board[i][j-1] == 1 || board[i][j+1] == 1 || board[i+1][j] == 1 || board[i-1][j] == 1 {
continue
}
// ์ผ๋จ ๊ฐ๊ฒฉ๊ณผ ์์น๋ฅผ ์ ์ฅํด๋์
let sum = price[i][j] + price[i][j+1] + price[i][j-1] + price[i+1][j] + price[i-1][j]
dict[[i, j]] = sum
}
}
if let minPos = dict.min(by: { $0.value <= $1.value }) {
ans += minPos.value
board[minPos.key[0]][minPos.key[1]] = 1
board[minPos.key[0]][minPos.key[1] + 1] = 1
board[minPos.key[0]][minPos.key[1] - 1] = 1
board[minPos.key[0] + 1][minPos.key[1]] = 1
board[minPos.key[0] - 1][minPos.key[1]] = 1
seeds -= 1
}
}
print(ans)
BOJ: #4195 - ์น๊ตฌ ๋คํธ์ํฌ
BFS/DFS ์ ํ์ ๋ฌธ์ ๋ผ๊ณ ์๊ฐํด์ ํ์๋๋ฐ, 31% ์ฏค์์ ์๊ฐ ์ด๊ณผ. ์ง๋ฌธ ๊ฒ์ํ์ ๋ณด๋๊น ์ด๋ฐ์์ผ๋ก ํธ๋ ๊ฑฐ ๊ฐ์ง ์์๋ฐ ๋ค๋ฅธ ๋ฐฉ์์ ์๊ฐ์ด ์ ๋์ ํจ์ค.
๋ฌธ์
https://www.acmicpc.net/problem/4195
๋ด๊ฐ ์์ฑํ ์ฝ๋
Swift
let t = Int(readLine()!)!
for _ in 0..<t {
let f = Int(readLine()!)!
var graph = [String: [String]]()
for _ in 0..<f {
let friend = readLine()!.split(separator: " ").map { String($0) }
// ์๋ฐฉํฅ ์น๊ตฌ ๊ด๊ณ
graph[friend[0], default: []].append(friend[1])
graph[friend[1], default: []].append(friend[0])
// ์น๊ตฌ ๊ด๊ณ๊ฐ ์๊ธธ ๋๋ง๋ค ๋คํธ์ํฌ ํ์ํ๊ธฐ
var visited: Set = [friend[0]]
var queue: Set = [friend[0]]
while !queue.isEmpty {
let v = queue.removeFirst()
for w in graph[v]! {
if !visited.contains(w) {
visited.insert(w)
queue.insert(w)
}
}
}
print(visited.count)
}
}