letinputs=readLine()!.map{Int(String($0))! }// Int
letinputs=readLine()!.map(String.init)// String
EOF(End of File) 처리
while let input =readLine(){...}
빠른 입력 처리 1
빠른 입력처리에서 결과를 보려면 control + D 키를 누름
접기/펼치기
finalclassFileIO{privateletbuffer:Dataprivatevarindex:Int=0init(fileHandle:FileHandle=FileHandle.standardInput){self.buffer =try! fileHandle.readToEnd()! // 인덱스 범위 넘어가는 것 방지}@inline(__always)privatefunc read()->UInt8{defer{ index +=1}
guard index < buffer.count else{return0}returnbuffer[index]}@inline(__always)func readInt()->Int{varsum=0varnow=read()varisPositive= 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{varstr=""varnow=read()
while now ==10 || now ==32{ now =read()}// 공백과 줄바꿈 무시
while now !=10 && now !=32 && now !=0{
str +=String(bytes:[now], encoding:.ascii)!
now =read()}return str
}}
숏코드
finalclassIO{varb:Data,i=0;init(fh:FileHandle=FileHandle.standardInput){b=try!fh.readToEnd()!}@inline(__always)func read()->UInt8{defer{i+=1};guard i<b.count else{return0};returnb[i]}@inline(__always)func readInt()->Int{vars=0,n=read(),p=true;while n==10||n==32{n=read()}
if n==45{p.toggle();n=read()};while n>=48,n<=57{s=s*10+Int(n-48);n=read()};returns*(p ?1:-1)}@inline(__always)func readString()->String{vart="",n=read();while n==10||n==32{n=read()}
while n !=10,n !=32,n !=0{t+=String(bytes:[n],encoding:.ascii)!;n=read()};return t}}
빠른 입력 처리 2
접기/펼치기
import Foundation
finalclassFileIO{@inline(__always)privatevarbuffer:[UInt8]=Array(FileHandle.standardInput.readDataToEndOfFile())+[0],byteIdx=0@inline(__always)privatefunc readByte()->UInt8{defer{ byteIdx +=1}return buffer.withUnsafeBufferPointer{$0[byteIdx]}}@inline(__always)func readInt()->Int{varnumber=0,byte=readByte(),isNegative= false
while byte ==10 || byte ==32{ byte =readByte()}
if byte ==45{ byte =readByte(); isNegative = true }
while 48...57~= byte { number = number *10+ Int(byte -48); byte =readByte()}return number *(isNegative ? -1:1)}@inline(__always)func readStringSum()->Int{varbyte=readByte()
while byte ==10 || byte ==32{ byte =readByte()}varsum=Int(byte)
while byte !=10 && byte !=32 && byte !=0{ byte =readByte(); sum +=Int(byte)}return sum - Int(byte)}@inline(__always)privatefunc write(_ output:String){FileHandle.standardOutput.write(output.data(using:.utf8)!)}}