Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
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
Tags
more
Archives
Today
Total
관리 메뉴

Olive Study Room

[백준 알고리즘] 7. 문자열 본문

cs/알고리즘

[백준 알고리즘] 7. 문자열

Olive Dev 2021. 6. 27. 00:45
 11654
let line = Character(readLine()!)
print(Int(line.asciiValue!))

 11654 - 2
print(Character(readLine()!).unicodeScalars.first!.value)

 11720
 런타임 에러
let line = Int(readLine()!)!
var line2 = Int(readLine()!)!
var sum: Int = 0
while line2 != 0 {
    sum += line2%10
    line2 /= 10
}
print(sum)

 11720 - 2
let line = Int(readLine()!)!
var line2 = readLine()!
var sum: Int = 0
line2.forEach({
    sum += Int(String($0))!
})
print(sum)

 10809
let line = readLine()!.map({String($0)}).map({Character($0).asciiValue!})
for i in Character("a").asciiValue!...Character("z").asciiValue! {
    if !line.contains(i) {
        print("-1", terminator: " ")
    }
    else {
        print(line.firstIndex(of: i)!, terminator: " ")
    }
}


 2675
let line = Int(readLine()!)!
for _ in 0..<line {
    var input = readLine()!.split(separator: " ").map({String($0)})
    var string = ""
    input[1].forEach( {
        string.append(String.init(repeating: $0, count: Int(input[0])!))
    })
    print(string.self)
}

 2675 - 2
let line = Int(readLine()!)!
for _ in 0..<line {
    var input = readLine()!.split(separator: " ").map({String($0)})
    input[1].forEach( {
        for i in 0..<Int(input[0])! {
            print($0, terminator: "")
        }
    })
    print("")
}

 1157
 런타임 오류
let line = String(readLine()!).lowercased().map({String($0)})
var arr2: [Int] = []
for i in 0..<line.count {
    var count = 0
    if line[i] == line[i+1] {
        count += 1
    }
    arr2.append(count)
}
if arr2.firstIndex(of: arr2.max()!)! != arr2.lastIndex(of: arr2.max()!)! {
    print("?")
}
else {
    print(line[arr2.firstIndex(of: arr2.max()!)!].uppercased())
}

 1157 - 2
 성공
let line = String(readLine()!).lowercased().map({String($0)})
var dic: [String: Int] = [:]
var arr: [String] = []
for i in 0..<line.count {
    if dic[line[i]] == nil {
        dic[line[i]] = 1
    }
    else {
        dic[line[i]]! += 1
    }
}
// 아래는 처음 사용했던 코드. max를 쓰지 않으려면 sort해줘야한다.
// let sortedDic = dic.sorted{ $0.1 > $1.1}
// = let sortedDic = dic.value.sorted(by: >)
for key in dic.keys {
    if dic[key] == dic.values.max() {
        arr.append(key)
    }
}
if arr.count > 1 {
    print("?")
}
else {
    print(Character(arr[0]).uppercased())
}

 1152
 print(readLine()!.split(separator: " ").count)

 2908
 다시 풀기
let line = readLine()!.split(separator: " ").map({Int($0)!})
var arr: [Int] = []
for i in 0..<line.count {
    arr.append((line[i]/100) + (((line[i])/10)%10)*10 + ((line[i])%10)*100)
}
print(arr.max()!)

 2908 - 2
 reverse 사용
let line = readLine()!.split(separator: " ").map({String($0)}).map({Int(String($0.reversed()))!})
print(line.max()!)

 5622
let line = readLine()!.map({String($0)})
var sum: Int = 0
for i in line {
    switch i {
    case "A", "B", "C" :
        sum += 3
    case "D", "E", "F" :
        sum += 4
    case "G", "H", "I" :
        sum += 5
    case "J", "K", "L" :
        sum += 6
    case "M", "N", "O" :
        sum += 7
    case "P", "Q", "R", "S" :
        sum += 8
    case "T", "U", "V" :
        sum += 9
    case "W", "X", "Y", "Z" :
        sum += 10
    default :
        sum = 0
    }
}
print(sum)

 5622 - 2
let line = readLine()!
var sum: Int = 0
for i in line {
    switch i {
    case "A", "B", "C" :
        sum += 3
    case "D", "E", "F" :
        sum += 4
    case "G", "H", "I" :
        sum += 5
    case "J", "K", "L" :
        sum += 6
    case "M", "N", "O" :
        sum += 7
    case "P", "Q", "R", "S" :
        sum += 8
    case "T", "U", "V" :
        sum += 9
    case "W", "X", "Y", "Z" :
        sum += 10
    default :
        sum = 0
    }
}
print(sum)

 2941
 런타임 오류
let line = readLine()!.map({String($0)})
var count: Int = line.count
for i in 0..<line.count {
    if line[i] == "c" && ((line[i+1] == "=")||(line[i+1] == "-")){
        count -= 1
    }
    else if line[i] == "d" && line[i+1] == "-" {
        count -= 1
    }
    else if line[i] == "l" && line[i+1] == "j" {
        count -= 1
    }
    else if line[i] == "n" && line[i+1] == "j" {
        count -= 1
    }
    else if line[i] == "z" && line[i+1] == "=" {
        if line[i-1] == "d" {
            count -= 2
        }
        else {
            count -= 1
        }
    }
    else if line[i] == "s" && line[i+1] == "=" {
        count -= 1
    }
}
print(count)

 2941 - 2
import Foundation
var line = readLine()!
let m = ["c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="]
for i in 0..<m.count {
    line = line.replacingOccurrences(of: m[i], with: "a")
}
print(line.count)

 1316
let d = Int(readLine()!)!
var result: Int = 0

for _ in 0..<d {
    var line = readLine()!
    var arr: [Character] = []
    for i in line {
        if !arr.contains(i) {
            arr.append(i)
        }
        else if arr.last != i{
            arr.removeAll()
            break
        }
    }
    if arr.count != 0 {
        result += 1
    }
}
print(result)


Comments