Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Olive Study Room

[백준 알고리즘] #2775번 : 수학, 런타임 오류 본문

cs/알고리즘

[백준 알고리즘] #2775번 : 수학, 런타임 오류

Olive Dev 2022. 4. 7. 15:48
런타임 오류는 비정상적인 종료로 인해 나는 것으로, 나는 optional을 강제 언랩핑하면서 출력했는데 nil이 받아져서 오류가 났다. 

 

var count = Int(readLine()!)!
var k: [Int] = []
var zerofloor: [Int] = []
var nextfloor: [Int] = []


for _ in 0..<count {
    let a = Int(readLine()!)!
    let b = Int(readLine()!)!
    
    zerofloor.removeAll()
    for i in 1...14 {
        zerofloor.append(i)
    }
    if a == 0 {
        k.append(b)
    }
    else {
        for _ in 0..<a {
            
            var sum: Int = 1
            nextfloor.removeAll()
            
            for j in 0..<b {
                if j == 0 {
                    nextfloor.append(1)
                }
                else {
                    sum += zerofloor[j]
                    nextfloor.append(sum)
                }
            }
            zerofloor = nextfloor
        }
        k.append(nextfloor.last!)
    }

}
for i in 0..<k.count {
    print(k[i])
}

 

 

위 코드는 a가 1일 때 nextfloor는 [1, 1+1, 2+(1+1), 3+ (2+(1+1))...] 이런 식으로 추가된다. 

그리고 그 값을 배열 채로 zerofloor에 저장하고 이를 호수까지만 반복해서 값을 구한다.

 

0호방의 처리와 sum에 (아랫층 이전 호수까지의 인원수)를 모두 더해 넣는 반복문이 관건이였다.

 

 

Comments