์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- ๋นํธ์ฐ์ฐ์
- ๋ถํ ์ ๋ณต
- ๋ฌธ์์ด
- Swift
- ๋ธ๋ฃจํธํฌ์ค
- WebView
- ์ ํด๋ฆฌ๋ ํธ์ ๋ฒ
- ์คํ
- ์ฝํ
- dfs
- BFS
- ๋ถํ ํ์
- ๋ถํ ์ ๋ณต
- ์๊ณ ๋ฆฌ์ฆ
- Queue
- ๋์ ํฉ
- SwiftUI
- ์ฝ๋ฉํ ์คํธ
- ๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ
- dp
- ์ด์งํ์
- ํ๋ก๊ทธ๋๋จธ์ค
- ๋ฐฑํธ๋ํน
- ์์
- ๋ฐฑ์ค
- WebApp
- ios
- ๋ธ๋ฃจํธํฌ์ค ์๊ณ ๋ฆฌ์ฆ
- Today
- Total
MUKER_DEV with iOS
[swift] ๋ฐฑ์ค 15552๋ฒ - ๋น ๋ฅธ A+B ๋ณธ๋ฌธ
๋ฌธ์
๋ณธ๊ฒฉ์ ์ผ๋ก for๋ฌธ ๋ฌธ์ ๋ฅผ ํ๊ธฐ ์ ์ ์ฃผ์ํด์ผ ํ ์ ์ด ์๋ค. ์ ์ถ๋ ฅ ๋ฐฉ์์ด ๋๋ฆฌ๋ฉด ์ฌ๋ฌ ์ค์ ์ ๋ ฅ๋ฐ๊ฑฐ๋ ์ถ๋ ฅํ ๋ ์๊ฐ์ด๊ณผ๊ฐ ๋ ์ ์๋ค๋ ์ ์ด๋ค.
C++์ ์ฌ์ฉํ๊ณ ์๊ณ cin/cout์ ์ฌ์ฉํ๊ณ ์ ํ๋ค๋ฉด, cin.tie(NULL)๊ณผ sync_with_stdio(false)๋ฅผ ๋ ๋ค ์ ์ฉํด ์ฃผ๊ณ , endl ๋์ ๊ฐํ๋ฌธ์(\n)๋ฅผ ์ฐ์. ๋จ, ์ด๋ ๊ฒ ํ๋ฉด ๋ ์ด์ scanf/printf/puts/getchar/putchar ๋ฑ C์ ์ ์ถ๋ ฅ ๋ฐฉ์์ ์ฌ์ฉํ๋ฉด ์ ๋๋ค.
Java๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค๋ฉด, Scanner์ System.out.println ๋์ BufferedReader์ BufferedWriter๋ฅผ ์ฌ์ฉํ ์ ์๋ค. BufferedWriter.flush๋ ๋งจ ๋ง์ง๋ง์ ํ ๋ฒ๋ง ํ๋ฉด ๋๋ค.
Python์ ์ฌ์ฉํ๊ณ ์๋ค๋ฉด, input ๋์ sys.stdin.readline์ ์ฌ์ฉํ ์ ์๋ค. ๋จ, ์ด๋๋ ๋งจ ๋์ ๊ฐํ๋ฌธ์๊น์ง ๊ฐ์ด ์ ๋ ฅ๋ฐ๊ธฐ ๋๋ฌธ์ ๋ฌธ์์ด์ ์ ์ฅํ๊ณ ์ถ์ ๊ฒฝ์ฐ .rstrip()์ ์ถ๊ฐ๋ก ํด ์ฃผ๋ ๊ฒ์ด ์ข๋ค.
๋ํ ์ ๋ ฅ๊ณผ ์ถ๋ ฅ ์คํธ๋ฆผ์ ๋ณ๊ฐ์ด๋ฏ๋ก, ํ ์คํธ์ผ์ด์ค๋ฅผ ์ ๋ถ ์ ๋ ฅ๋ฐ์์ ์ ์ฅํ ๋ค ์ ๋ถ ์ถ๋ ฅํ ํ์๋ ์๋ค. ํ ์คํธ์ผ์ด์ค๋ฅผ ํ๋ ๋ฐ์ ๋ค ํ๋ ์ถ๋ ฅํด๋ ๋๋ค.
์์ธํ ์ค๋ช ๋ฐ ๋ค๋ฅธ ์ธ์ด์ ๊ฒฝ์ฐ๋ ์ด ๊ธ์ ์ค๋ช ๋์ด ์๋ค.
์ด ๋ธ๋ก๊ทธ ๊ธ์์ BOJ์ ๊ธฐํ ์ฌ๋ฌ ๊ฐ์ง ํ์ ๋ณผ ์ ์๋ค.
์ ๋ ฅ
์ฒซ ์ค์ ํ ์คํธ์ผ์ด์ค์ ๊ฐ์ T๊ฐ ์ฃผ์ด์ง๋ค. T๋ ์ต๋ 1,000,000์ด๋ค. ๋ค์ T์ค์๋ ๊ฐ๊ฐ ๋ ์ ์ A์ B๊ฐ ์ฃผ์ด์ง๋ค. A์ B๋ 1 ์ด์, 1,000 ์ดํ์ด๋ค.
์ถ๋ ฅ
๊ฐ ํ ์คํธ์ผ์ด์ค๋ง๋ค A+B๋ฅผ ํ ์ค์ ํ๋์ฉ ์์๋๋ก ์ถ๋ ฅํ๋ค.
์์ ์ ๋ ฅ 1
5
1 1
12 34
5 500
40 60
1000 1000
์์ ์ถ๋ ฅ 1
2
46
505
100
2000
- readLine()์ ์ฌ์ฉํ๋ฉด ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ๋ค. swift์์๋ ๋ฐ๋ก ํด๋์ค๋ฅผ ๋ง๋ค์ด์ค์ ํ์ด์ผ ์๊ฐ์ ํ์์ ํต๊ณผํ ์ ์๋ค.
- ๋ค๋ฅธ๋ถ์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํด์ ํ์๋ค. ์ถ์ฒ : (Swift) 15552 ๋น ๋ฅธ A + B (velog.io)
- ์ผ๋ถ๋ฌ ์ฃผ์์ ๋ค ์ง์ ์ผ๋, ์ถ์ฒํ์ด์ง์์ ์ฐธ๊ณ ํ๋ฉด ๋๋ค.
import Foundation
class FileIO {
private let buffer:[UInt8]
private var index: Int = 0
init(fileHandle: FileHandle = FileHandle.standardInput) {
self.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 fileIO = FileIO()
let T = fileIO.readInt()
for _ in 0..<T {
let A = fileIO.readInt()
let B = fileIO.readInt()
print(A + B)
}
94720kb, 836ms, 1647b
'๐ค ์๊ณ ๋ฆฌ์ฆ > BAEKJOON' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[swift] ๋ฐฑ์ค 11022๋ฒ: A+B -8 (0) | 2022.08.27 |
---|---|
[swift] ๋ฐฑ์ค 11021๋ฒ - A+B -7 (0) | 2022.08.27 |
[swift] ๋ฐฑ์ค 25304๋ฒ - ์์์ฆ (0) | 2022.08.26 |
[swift] ๋ฐฑ์ค 8393๋ฒ - ํฉ (0) | 2022.08.26 |
[swift] ๋ฐฑ์ค 10950๋ฒ - A+B - 3 (0) | 2022.08.22 |