import Foundation
let number = Int(readLine()!)!
for i in 1...9 {
print("\(number) * \(i) = \(number*i)")
}
let line = readLine()!
let intArr = line.components(separatedBy: " ").map({Int($0)!})
let line = readLine()!
let stringArr = line.components(separatedBy: " ").map({String($0)!})
let stringToInt = Array(readLine()!).map({Int(String($0))!})
15552
fread를 활용하라는데
@inline(__always)라구요?
무슨 말인지 모르겠는데요?
import Foundation
final class FileIO {
private let buffer:[UInt8]
private var index: Int = 0
init(fileHandle: FileHandle = FileHandle.standardInput) {
buffer = Array(try! fileHandle.readToEnd()!)+[UInt8(0)] // 인덱스 범위 넘어가는 것 방지
}
@inline(__always) private func read() -> UInt8 {
defer { index += 1 }
return buffer[index]
}
@inline(__always) func readInt() -> Int {
var sum = 0
var now = read()
var isPositive = true
while now == 10
|| now == 32 { now = read() } // 공백과 줄바꿈 무시
if now == 45 { isPositive.toggle(); now = read() } // 음수 처리
while now >= 48, now <= 57 {
sum = sum * 10 + Int(now-48)
now = read()
}
return sum * (isPositive ? 1:-1)
}
@inline(__always) func readString() -> String {
var now = read()
while now == 10 || now == 32 { now = read() } // 공백과 줄바꿈 무시
let beginIndex = index-1
while now != 10,
now != 32,
now != 0 { now = read() }
return String(bytes: Array(buffer[beginIndex..<(index-1)]), encoding: .ascii)!
}
@inline(__always) func readByteSequenceWithoutSpaceAndLineFeed() -> [UInt8] {
var now = read()
while now == 10 || now == 32 { now = read() } // 공백과 줄바꿈 무시
let beginIndex = index-1
while now != 10,
now != 32,
now != 0 { now = read() }
return Array(buffer[beginIndex..<(index-1)])
}
}
let fIO = FileIO()
let n = fIO.readInt()
var str = ""
for _ in 0..<n {
str += "\(fIO.readInt() + fIO.readInt())\n"
}
print(str)
2742
for문 거꾸로 찍기
import Foundation
let line = readLine()!
let intVal = Int(line)!
for i in stride(from: intVal, to: 1, by: -1) { //to가 0까지해야 5 4 3 2 1 이 나온다.
print("\(i)")
}
stride 정리
stride(from:1, to:5, by:2) // open range..5 불포함 1,3 까지만
stride(from:1, through:5, by:2) // closed range..5 포함 1,3,5까지
2438
별찍기
String(repeating: count: ) 요런 메서드가 있다
import Foundation
let line = readLine()!
let intVal = Int(line)!
for i in 1 ... intVal {
print(String(repeating: "*", count: i))
}
배열 초기화에 자주 쓴다
[Int](repeating: count: )
[[Int]](repeating: [Int](repeating: count: ), count: )