구현(1)에 이어서 구현(2)를 풀 생각이다. 감을 조금씩 찾는 것 같긴 한데, 아직 어색하긴 하다.

DP, BFS, DFS 등 한창 공부했다가 지금 거의 다 휘발된 것 같다. 얼른 구현유형으로 익숙해지고, 나머지 다른 유형들도 감 되찾아야겠다.

 


문제 풀이를 할 때 마다 계속 추가됩니다.

cmd + F 를 통해 문제번호 찾기를 추천드립니다.

 

22.11.15 업데이트

22.11.23 업데이트

 

아래 깃허브 주소에서 모든 백준 문제풀이를 확인하실 수 있습니다.

https://github.com/SuminPark-developer/BaekJoonPS

 

GitHub - SuminPark-developer/BaekJoonPS: 백준 Swift PS

백준 Swift PS. Contribute to SuminPark-developer/BaekJoonPS development by creating an account on GitHub.

github.com

문제 리스트

https://github.com/tony9402/baekjoon/tree/main/implementation

문제 리스트 - 구현(2) 파트(실버)


백준 Swift 20546번

문제 유형 : 구현

난이도 : 실버5

실버5인데 뭔가 잘 안풀렸다... 좀 더 잘 생각해보자.

인덱스가 벗어날 것 같으면, 미리 인덱스를 더 가서 for문을 돌리면 됨!!!

 

// MARK: - 20546번
let money = Int(readLine()!)!
let stocks: [Int] = readLine()!.split(separator: " ").map{Int(String($0))!}

var updown: [String] = ["="]
for i in 1..<stocks.count {
    if stocks[i] > stocks[i - 1] {
        updown.append("+")
    }
    else if stocks[i] < stocks[i - 1] {
        updown.append("-")
    }
    else {
        updown.append("=")
    }
}
//print(updown)

var (junMoney, seoungMoney) = (money, money)
var (junCount, seoungCount) = (0, 0)

for stock in stocks {
    junCount += junMoney / stock
    junMoney = junMoney % stock
}

for i in 2..<stocks.count {
//    if stocks[i] > seoungMoney { // 성민이 - 빚내서 주식하지 않음.
//        continue
//    }
    
    if updown[i] == "=" {
        continue
    }
    
    if updown[i] == "+" && updown[i - 1] == "+" && updown[i - 2] == "+" { // 전량매도
        seoungMoney += seoungCount * stocks[i]
        seoungCount = 0
    }
    else if updown[i] == "-" && updown[i - 1] == "-" && updown[i - 2] == "-" { // 전량매수
        seoungCount += seoungMoney / stocks[i]
        seoungMoney = seoungMoney % stocks[i]
    }
//    print(i, seoungMoney, seoungCount)
}

var (junSum, seoungSum) = (0, 0)
junSum = junMoney + junCount * stocks.last!
seoungSum = seoungMoney + seoungCount * stocks.last!

if junSum > seoungSum {
    print("BNP")
}
else if junSum < seoungSum {
    print("TIMING")
}
else {
    print("SAMESAME")
}

 


백준 Swift 2578번

문제 유형 : 구현

난이도 : 실버4

// MARK: - 2578번
var board: [[Int]] = []
var bingo: [[Bool]] = Array(repeating: Array(repeating: false, count: 5), count: 5)

for _ in 0..<5 {
    board.append(readLine()!.split(separator: " ").map{Int(String($0))!})
}

var calls: [Int] = []
for _ in 0..<5 {
    let temp: [Int] = readLine()!.split(separator: " ").map{Int(String($0))!}
    calls += temp
}

func findIndex(_ num: Int) -> (Int, Int) {
    for y in 0..<5 {
        for x in 0..<5 {
            if board[y][x] == num {
                return (y, x)
            }
        }
    }
    return (-1, -1) // 여기는 올 일 없음.
}

func bingoCount() -> Int {
    var lineCount: Int = 0
    
    for y in 0..<5 { // 가로줄 빙고 체크.
        var tempCount: Int = 0
        for x in 0..<5 {
            if bingo[y][x] == false {
                break
            }
            else {
                tempCount += 1
            }
            
            if tempCount == 5 { // 빙고면,
                lineCount += 1
            }
        }
    }
    
    for x in 0..<5 { // 세로줄 빙고 체크.
        var tempCount: Int = 0
        for y in 0..<5 {
            if bingo[y][x] == false {
                break
            }
            else {
                tempCount += 1
            }
            
            if tempCount == 5 { // 빙고면,
                lineCount += 1
            }
        }
    }
    
    if (bingo[0][0] == true) && (bingo[1][1] == true) && (bingo[2][2] == true) && (bingo[3][3] == true) && (bingo[4][4] == true) {
        lineCount += 1
    }
    
    if (bingo[0][4] == true) && (bingo[1][3] == true) && (bingo[2][2] == true) && (bingo[3][1] == true) && (bingo[4][0] == true) {
        lineCount += 1
    }
    
    return lineCount
}

var count: Int = 0
for call in calls {
    count += 1
    
    let (y, x) = findIndex(call)
    bingo[y][x] = true
    
    if bingoCount() >= 3 {
        print(count)
        break
    }
}

 


 

반응형