전체 글
-
[백준 10951][Swift] 두 정수 A와 B를 입력받은 다음, A+B를 출력 -4알고리즘 2022. 3. 31. 16:08
코드 while let input = readLine() { let inputNumber = input.split(separator: " ").map { Int($0)! } print(inputNumber[0] + inputNumber[1]) } EOF(End Of File) 문제 무한루프가 돌 때 어떻게 빠져나올지 묻는 문제 while true { ... } readLine() 자체를 true인지 false 인지 판단 control + d 로 빠져나오거나 enter 치면 빠져나옴 https://www.acmicpc.net/problem/10951 10951번: A+B - 4 두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오. www.acmicpc.net
-
[백준 10950][Swift] 두 정수 A와 B를 입력받은 다음, A+B를 출력 -3카테고리 없음 2022. 3. 31. 14:39
코드 let inputT = Int(readLine()!)! for _ in 1...inputT { let inputNum = readLine()?.split(separator: " ") let a = Int(inputNum?.first ?? "0")! let b = Int(inputNum?.last ?? "0")! print(a + b) } 혹은 map 메소드 이용 let inputT = Int(readLine()!)! for _ in 1...inputT { let inputNum = (readLine()?.split(separator: " ").map { Int($0)! })! print(inputNum[0] + inputNum[1]) } https://www.acmicpc.net/problem/10..
-
[백준 1000][Swift] 두 정수 A와 B를 입력받은 다음, A+B를 출력알고리즘 2022. 3. 31. 10:37
코드 import Foundation let input = readLine()?.components(separatedBy: " ") if let a = Int(input?.first ?? "0"), let b = Int(input?.last ?? "0") { print(a + b) } 혹은 let input = readLine()?.split(separator: " ") let first = Int(input?.first ?? "0")! let second = Int(input?.last ?? "0")! print(first + second) https://www.acmicpc.net/problem/1000 1000번: A+B 두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오. ..
-
[Swift] Optional(옵셔널)Swift 2022. 3. 17. 20:58
옵셔널은 enum이다. enum Optional { case None // not set case Some // set, T는 연관값 } Optional에서 는 옵셔널이 모든 타입의 옵셔널이 될 수 있다는 뜻 let x: String? = nil // = Optional.None let x: String? = "hello" // = Optional.Some("hello"), 연관값 hello 옵셔널을 unwrapped 하는 방법 1. switch를 사용해서 옵셔널을 unwrapped해 연관값을 가져오는 방법 switch x { case Some(let value): y = value case None: // error: raise an exception } 값이 없으면 런타임 오류가 발생함 2. if l..