(세 자리 수) × (세 자리 수)는 다음과 같은 과정을 통하여 이루어진다.
(1)과 (2)위치에 들어갈 세 자리 자연수가 주어질 때 (3), (4), (5), (6)위치에 들어갈 값을 구하는 프로그램을 작성하시오.
첫째 줄에 (1)의 위치에 들어갈 세 자리 자연수가, 둘째 줄에 (2)의 위치에 들어갈 세자리 자연수가 주어진다.
첫째 줄부터 넷째 줄까지 차례대로 (3), (4), (5), (6)에 들어갈 값을 출력한다.
472
385
2360
3776
1416
181720
Swift는 막강한 기술을 지원한다고 하지만...문자열 요소의 접근이 생각보다 까다로웠다.
어렵지 않게 생각한 이 문제는
let A = "123"
print(A[0])
를 호출하였을때 당연하게도....1이 출력될거라 생각했다.
될줄 알았어!!! 왜 안돼!!!! 파이썬 녀석은 됐는데!!!! Swift!!!
고맙게도...블로그에 String 관련 함수들을 잘 정리해주신분이 있었다.
해당 블로그 링크
도전정신을 가지고 Subscript을 이용해보자
- 서브스크립트는 시퀀스나 딕셔너리 등 대괄호 [] 안에 특정 값을 넣어 원하는 값을 바로 찾아오는 기능이다.
startIndex : 문자열의 시작 요소 인덱스를 가리킨다.
endIndex : 문자열의 마지막 요소 인덱스 다음을 가리킨다.
index(before: String.Index) : 인자로 들어온 인덱스 1칸 앞을 가리킨다.
index(after: String.Index) : 인자로 들어온 인덱스 1칸 뒤를 가리킨다.
index(String.Index, offsetBy: String.IndexDistance) : 인자로 들어온 인덱스와 offsetBy 차이만큼 떨어진 곳을 가리킨다.
firstIndex(of: Character), lastIndex(of: Character) : 인자로 들어온 문자가 몇번째 인덱스에 있는지 (Optional)- 불러올경우 다 Character 형이다.
let str = "ABCDE"
str[str.startIndex] // A
str[str.index(after: str.startIndex)] // B
str[str.index(before: str.endIndex)] // E
str[str.index(str.startIndex, offsetBy: 2] // C
str[str.index(str.endIndex, offsetBy: -2] // D
이것을 토대로
str[str.index(str.startIndex, offsetBy: 2]을 이용하면..?
변수명[변수명.index(변수명.startIndex, offsetBy: 자리수)]로 바꿔서 쓰면된다까지 유추해냈다.
나중에 extension을 배우고 나면 꼭...벨로그에 설정해야하는것에 정리를 해둬야겠다.
근데..코딩 테스트때는 어림도 없을테니까... 기본으로 사용해보자 ㅠㅠ...
let in1 = Int(readLine()!)!, in2 = Int(readLine()!)!
let n1 = in2%10, n2 = (in2%100)/10, n3 = in2/100
print (in1 * n1, in1 * n2, in1 * n3, in1 * in2)
let input1 = Int(readLine()!)!
let input2 = readLine()!
let B0 = Int(String(input2[input2.index(input2.startIndex, offsetBy: 0)]))!
let B1 = Int(String(input2[input2.index(input2.startIndex, offsetBy: 1)]))!
let B2 = Int(String(input2[input2.index(input2.startIndex, offsetBy: 2)]))!
let C3 = input1 * B2
let C4 = input1 * B1
let C5 = input1 * B0
let C6 = input1
print(C3)
print(C4)
print(C5)
print(C6)
짧게 바꿔본것
let input1 = Int(readLine()!)!, input2 = readLine()! let B0 = Int(String(input2[input2.index(input2.startIndex, offsetBy: 0)]))! let B1 = Int(String(input2[input2.index(input2.startIndex, offsetBy: 1)]))! let B2 = Int(String(input2[input2.index(input2.startIndex, offsetBy: 2)]))! print(input1 * B2, input1 * B1, input1 * B0, input1 * (Int(input2)!))
print를 맵을 이용해서 바꿔본것
let input1 = Int(readLine()!)!, input2 = readLine()!
let B0 = Int(String(input2[input2.index(input2.startIndex, offsetBy: 0)]))!
let B1 = Int(String(input2[input2.index(input2.startIndex, offsetBy: 1)]))!
let B2 = Int(String(input2[input2.index(input2.startIndex, offsetBy: 2)]))!
[B2, B1, B0, Int(input2)!].map{print($0*input1)}
let input1 = Int(readLine()!)!
let input2 = readLine()!
let B0 = Int(String(input2[input2.index(input2.startIndex, OffsetBy: 0)]))!
let B1 = Int(String(input2[input2.index(input2.startIndex, OffsetBy: 1)]))!
let B2 = Int(String(input2[input2.index(input2.startIndex, OffsetBy: 2)]))!
let C3 = input1 * B2
let C4 = input1 * B1
let C5 = input1 * B0
let C6 = C3 + C4 * 10 + C5 * 100
print(C3)
print(C4)
print(C5)
print(C6)
컴파일 에러가 뜬다. 근데....Swift에서는 작동한다. 이유를 모르겠네..ㅠㅠ...
offsetBy를 OffsetBy로 써서 틀린거였다..하아..ㅠㅠㅠ!! 몇번을 봤는데 맞추고 다시 봤을때보였다...
let i=Int(readLine()!)!,j=Int(readLine()!)!;[j%10,j/10%10,j/100,j].map{print($0*i)}
j/10%10 처럼 하는 방법이 있구나..! 생각보다 %와 /에 개념이 부족한걸 알게된 문제..ㅠㅠ..
index 관련으로 공부한건 좋지만 좀더 간단한 방법이 있었다는걸 너무 늦게 깨달았다 ㅠ