728x90
๋ฐ์ํ
Problem
Solution
1. ๋ฌธ์๋ฅผ ์ ๋ ฅ๋ฐ๊ณ ๊ณต๋ฐฑ ๊ธฐ์ค์ผ๋ก ๋๋๋ค.
let line = readLine() ?? ""
let lineArr = line.split(separator: " ").map{String($0)}
2. ๋ ๋ฌธ์๋ฅผ Array๋ก ๋ง๋ค๊ณ ๋ค์ง์ด์ค๋ค.
var A = Array(lineArr[0].map{String($0)}.reversed())
var B = Array(lineArr[1].map{String($0)}.reversed())
3. ๋ ๊ธด ๋ฌธ์๊ฐ ์๋ค๋ฉด ๋ค์ ์ฐจ์ด๋งํผ 0์ ์ถ๊ฐํด์ค๋ค.
var isALong = A.count > B.count
let addZero = Array(repeating: "0", count: abs(A.count-B.count))
A = isALong ? A : A + addZero
B = isALong ? B + addZero : B
4. for๋ฌธ์ผ๋ก ๋์์๋ถํฐ ๋ ์ซ์๋ฅผ ๋ํด์ค๋ค.
๋ง์ฝ ๋ํ ์ซ์๊ฐ 10์ ๋๋๋ค๋ฉด one์ ์ ์ฅํ์ฌ ๋ค์ ์ซ์๋ฅผ ๋ํ ๋ ๋ํด์ค๋๋ค.
๊ทธ๋ฆฌ๊ณ ๊ฐ์ฅ ๋ง์ง๋ง ์ซ์๋ 10์ผ๋ก ๋๋ ๊ฒ์ด ์๋ ๊ทธ๋๋ก ๋ฃ์ด์ค๋๋ค.
var one = 0
var answer:[String] = []
let count = isALong ? A.count : B.count
for i in 0..<count {
let sum = Int(A[i])! + Int(B[i])! + one
one = sum/10
if i == count - 1 {
answer.append(String(sum))
}else {
answer.append(String(sum%10))
}
}
5. ์ ๋ต์ ๋ค์ง๊ณ ๋ฌธ์๋ก ๋ง๋ค์ด ์ถ๋ ฅํ๋ค.
print(Array(answer.reversed()).joined())
Source Code
P.S
์ค๋๋ง์ ๋ฐฑ์ค ๋ฌธ์ ๋ฅผ ํ์๋ค.
'๊ฐ๋ณ๊ฒ ๋ธ๋ก ์ฆ 5๋ ํ์ด๋ณผ๊น?' ๋ผ๋ ๋ง์ธ๋๋ก ํ์๋๋ฐ ์... ์๊ฐ๋ณด๋ค ์ด๋ ค์ ๋ค...
๋ญ๊ฐ ํ๋ก๊ทธ๋๋จธ์ค ๋ฌธ์ ๊ฐ ์ํ๋ ๊ฒ๊ณผ ๋ฐฑ์ค์ด ์ํ๋ ๊ฒ์ด ์ข ๋ค๋ฅธ ๊ฒ ๊ฐ๋ค...
728x90
๋ฐ์ํ
'๐ Problem Solution' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] Baekjoon ํฐ๋ฆฐ๋๋กฌ ๋ง๋ค๊ธฐ 1254๋ฒ (0) | 2022.01.06 |
---|---|
[Swift] Baekjoon ์ต๋๊ฐ 2562๋ฒ (0) | 2022.01.06 |
[Swift] 2020 KAKAO BLIND RECRUITMENT ์ธ๋ฒฝ ์ ๊ฒ (0) | 2021.09.08 |
[Swift] 2018 KAKAO BLIND RECRUITMENT [1์ฐจ] ์ถ์ ํธ๋ํฝ (0) | 2021.05.19 |
[Swift] 2020 KAKAO INTERNSHIP ํคํจ๋ ๋๋ฅด๊ธฐ (0) | 2021.05.08 |
๋๊ธ