반응형
문제
https://www.acmicpc.net/problem/27866
27866번: 문자와 문자열
첫째 줄에 영어 소문자와 대문자로만 이루어진 단어 S가 주어진다. 단어의 길이는 최대 1000이다. 둘째 줄에 정수 i가 주어진다. (1≤i≤|S|)
www.acmicpc.net
풀이
입력받은 문자열의 i번째 글자를 출력하면 되는 문제
Swift에서는 index로 문자열에 접근할 수 없기 때문에, 문자열을 배열형태로 만들거나, String의 index 메서드를 활용하여 풀 수 있음
index는 1이 아닌 0부터 시작하므로 입력받은 i에서 1을 빼주어서 구해주어야 함
소스코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let s = readLine()!.map { $0 } | |
let i = Int(readLine()!)! - 1 | |
print(s[i]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let s = readLine()! | |
let i = Int(readLine()!)! - 1 | |
print(s[s.index(s.startIndex, offsetBy: i)]) |
후기
Swift에서 index로 문자열에 접근할 수 없어서 당황할 수 있지만, 쉽게 풀 수 있는 문제
반응형
'PS > 백준' 카테고리의 다른 글
[BOJ] 백준 1259 팰린드롬수 (Swift) (0) | 2023.12.30 |
---|---|
[BOJ] 백준 2635 수 이어가기 (Swift) (1) | 2023.12.27 |
[BOJ] 백준 2920 음계 (Swift) (1) | 2023.12.19 |
[BOJ] 백준 2741 N 찍기 (Swift) (0) | 2023.12.11 |
[BOJ] 백준 2577 숫자의 개수 (Swift) (1) | 2023.12.11 |