feldblume5263 / TIL

Today I Learned
0 stars 0 forks source link

(220817)[개인 공부] 백준 3.1 - 3.? (1시간) #4

Closed feldblume5263 closed 2 years ago

feldblume5263 commented 2 years ago

1시간 동안 문제 풀기 재미보다는 가독성과 시간복잡도를 더 신경쓰자.

feldblume5263 commented 2 years ago

2739

let n = Int(readLine()!)!
for i in 1 ... 9 { print("\(n) * \(i) = \(n * i)") }

10950

let c = Int(readLine()!)!
var k: [Int] = []
for _ in 0 ..< c {
    k.append(readLine()!.split(separator: " ").map{Int($0)!}.reduce(0){$0 + $1})
}
for int in k {
    print(int)
}

8393

print(Array(1 ... Int(readLine()!)!).reduce(0) {$0 + $1})

25304

let sum = Int(readLine()!)!
var real = 0
for _ in 0 ..< Int(readLine()!)! {
    real += readLine()!.split(separator: " ").map{Int($0)!}.reduce(1, *)
}
if real == sum { print("Yes") } else { print("No") }

15552

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()
for _ in 1 ... fIO.readInt() {
    print(fIO.readInt() + fIO.readInt())
}