inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

10주완성 C++ 코딩테스트 | 알고리즘 코딩테스트

2-i 수학숙제 Swift 풀이

250

뉴원

작성한 질문수 1

0

기본 로직은 큰돌님과 같이
1. 처음에 0 이 있다면 앞에서 모두 뺴기
2. 비교는 size -> String 각각 비교로 진행하였습니다.


위의 코드는 String 을 Array 로 받은 후, 직접 0을 걸렀고

밑의 코드는 내부에서 0을 걸렀습니답.

둘 다 예제나 스스로 만들어본 반례 모두 통과하는데, 위의 코드는 통과하고 밑의 코드는 실패해서
언어의 특성때문인지, 제가 놓치는 알고리즘적인 요소가 있는지 궁금해서 여쭤봅니다.

* 그리고 혹시, 이렇게 다른 언어를 여쭤봐도 괜찮은가요 ..?
특히 Swift 로 다시 풀어볼 때 많이 틀리는 것 같아서 질문 올려봅니답 ㅠㅠ..

통과코드 :

var testCase = Int(readLine()!)!
var result = [String]()
var isZero = false

while testCase > 0 {
    let input = Array(readLine()!)
    var integer = ""
    
    for i in 0 ..< input.count {
        if integer == "" && i != input.count - 1 && input[i] == "0" && (input[i + 1] >= "0" && input[i + 1] <= "9")  {
            continue
        }
        
        if input[i] >= "0" && input[i] <= "9" {
            integer += String(input[i])
            
            if i == input.count - 1 {
                result.append(integer)
            }
            else if input[i + 1] > "9" || input[i + 1] < "0" {
                result.append(integer)
                integer = ""
            }
        }
    }
    
    testCase -= 1
}

result.sort{ (first, second) -> Bool in
    if first.count == second.count {
        return first < second
    } else {
        return first.count < second.count
    }
}

result.forEach{
    print($0)
}


실패코드:

let N = Int(readLine()!)!
var result = [String]()

for _ in 0 ..< N{
    let input = readLine()!
    var tempValue = ""
    
    for i in input.indices {
        
        switch input[i].asciiValue! {
        case Character("0").asciiValue! ... Character("9").asciiValue!:
            if i == input.index(before: input.endIndex) {
                while !tempValue.isEmpty && tempValue.first == "0" {
                    tempValue.removeFirst()
                }
                result.append(tempValue + String(input[i]))
                continue
            }
            tempValue += String(input[i])

        default:
            if tempValue == "" {
                continue
            }
            
            while !tempValue.isEmpty && tempValue.first == "0" {
                tempValue.removeFirst()
            }
            result.append(tempValue)
            tempValue = ""
        }
    }
}

result.sorted {
    $0.count == $1.count ? $0 < $1 : $0.count < $1.count
}.forEach {
    print($0)
}

 

코딩-테스트 코테 준비 같이 해요! swift C++

답변 1

1

큰돌

안녕하세요 ㅎㅎ

C++이외에 언어는 저도 파악이 어렵기 때문에 답변 불가합니다. ㅠㅠ

감사합니다.

코딩살구클럽 가입 문의

0

29

2

코딩 살구 클럽 컴파일 에러

0

19

1

추천 문제

0

18

1

코딩살구클럽 승인

0

25

1

코살구 1주차 1940번 문제 조건과 프라이빗 테스트 불일치 문의

0

34

2

문제를 고민하는 시간 관련

0

27

2

코딩살구클럽

0

42

2

코딩살구클럽 문의

0

45

2

코딩살구클럽 승인

0

37

2

DP 경우의 수 설명이 이해가 되지 않습니다.

0

35

2

3-F 채점 관련 질문

0

32

1

BFS, DFS 활용이 되는 상황에서의 방향성

0

34

2

코딩살구클럽 승인

0

46

2

코딩살구클럽승인

0

39

3

코딩살구클럽 승인

0

56

2

3-D 관련 질문

0

35

2

코살구 회원가입 문의

0

45

2

코살구 로그인 문제

0

65

2

3-A 문제 풀이 관련 질문

0

56

3

2-O 질문 있습니다

0

38

2

2-T 문제에 관한 질문

0

40

2

코딩 살구 클럽 접속 및 사용방법 문의

0

66

2

안녕하세요~. 현재 코살코딩클럽 사이트가 접속이 안됩니다~

0

67

2

코딩살구클럽 로그인문제

0

85

3