Olive Study Room
[백준 알고리즘] 7. 문자열 본문
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)
'cs > 알고리즘' 카테고리의 다른 글
[백준 알고리즘] #2775번 : 수학, 런타임 오류 (0) | 2022.04.07 |
---|---|
[백준 알고리즘] #4673번 : Dictionary, 시간초과, 런타임 오류 (0) | 2021.06.30 |
[백준 알고리즘] 6. 함수 (0) | 2021.06.26 |
[백준 알고리즘] #4673번 : 셀프넘버, set.remove (0) | 2021.06.25 |
[백준 알고리즘] #8958번 : 가우스 함수, compactMap, reduce (0) | 2021.06.23 |
Comments