1. ๋ฌธ์
2. ์ ์ถ๋ ฅ
3. ์ ์ถ๋ ฅ ์์
4. ๋ฌธ์ ์ค๊ณ
- Swift์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ Integer ์๋ฃํ์ ์ต๋ ํฌ๊ธฐ๋ 21์ต์ด๋ฏ๋ก 10^100 ์ซ์๋ฅผ ๋ด์ ์ ์์
- ๋ฐ๋ผ์ ๋ฐฐ์ด์ ๊ฐ ์๋ฆฟ์๋ฅผ ๋ด๊ณ , ์ฐ์ฐ์ ์งํํ๊ณ ๋ฌธ์์ด์ ์ ์ฅํจ์ผ๋ก์จ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์
- ๋จผ์ ๋ ํฐ ์ซ์๊ฐ ๋ฌด์์ธ์ง ๊ธธ์ด๋ฅผ ํตํด ๋ ํฐ ์ซ์๋ฅผ ํ์ ํ ์ ์์.
- ๊ทธ ํ, ๊ฐ ์ซ์์ ํด๋นํ๋ ๋ฐฐ์ด์ ์ซ์๋ฅผ ๊ฐ ์๋ฆฟ์ ๋ง๋ค ๋ด์
- ์ฌ๊ธฐ์ ๊ณฑ์ ๊ฒฐ๊ณผ๋ ๋ ํฐ ์ซ์์ ๊ธธ์ด + (์์ ์ซ์์ ๊ธธ์ด - 1) ์ธ ๊ฒ์ ์ ์ ์๊ณ , ์์ ์ซ์์ ๊ฐ ์๋ฆฌ์์ ํฐ ์ซ์๋ฅผ ๊ณฑํ ๊ฐ์
- ํ ์นธ์ฉ ์์ผ๋ก ๋ฐ๋ ค๋๊ฐ๋ฉฐ ๋ํด์ง๋ ํจํด์ ํ์ ํ ์ ์์
- ๊ฐ๋ น, 123 * 12์ ๊ฒฝ์ฐ 123 * 2 = 246 + 123 * 1 = 1230 ๊ฐ์ ๊ฒฝ์ฐ๋ฅผ ๋ณด๋ฉด ์ดํดํ๊ธฐ ์ฌ์
- ๊ณฑ์ ์ฐ์ฐ์ ๋์ ํด์ ๋ํด์ ๊ณฑ์ ๋ฐฐ์ด์ ์ ์ฅ ํ, 10์ ๋์ด๊ฐ๋ ์ซ์๋ค์ 10์ผ๋ก ๋๋ ๋ชซ์ ์ ์๋ฆฌ์ ๋๊ฒจ์ฃผ๊ณ , 10์ผ๋ก ๋๋ ๋๋จธ์ง๋ฅผ ํ์ฌ ์๋ฆฌ์ ์ ์ฅํจ์ผ๋ก์จ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํจ
5. ์ ์ฒด ์ฝ๋
//
// main.swift
// BigNumberMultiple
//
// Created by ์ด์์ฌ on 2022/03/10.
//MARK: - ํฐ ์๋ฆฟ์ ๊ณฑ์
//MARK: - Framework
import Foundation
//MARK: - Function
func solution() -> Void {
//MARK: - Input
guard let A: String = readLine() else { return }
guard let B: String = readLine() else { return }
var answer: String = ""
var smallNumberIndex: Int = 0
var flag: Bool = false
var multiple: [Int] = []
var bigNumber: [Int] = []
var smallNumber: [Int] = []
//MARK: - Process
if A.count < B.count {
flag = true
}
if flag {
multiple = Array(repeating: 0, count: B.count + (A.count - 1))
bigNumber = Array(repeating: 0, count: B.count)
smallNumber = Array(repeating: 0, count: B.count)
for i in stride(from: B.count - 1, through: 0, by: -1) {
bigNumber[i] = Int(B[B.index(B.startIndex, offsetBy: i)].asciiValue! - 48)
}
smallNumberIndex = A.count - 1
for i in stride(from: B.count - 1, through: B.count - A.count, by: -1) {
smallNumber[i] = Int(A[A.index(A.startIndex, offsetBy: smallNumberIndex)].asciiValue! - 48)
smallNumberIndex -= 1
}
smallNumberIndex = A.count - 1
for i in stride(from: B.count - 1, through: B.count - A.count, by: -1) {
for j in stride(from: B.count - 1, through: 0, by: -1) {
multiple[smallNumberIndex + j] += bigNumber[j] * smallNumber[i]
}
smallNumberIndex -= 1
}
} else {
multiple = Array(repeating: 0, count: A.count + (B.count - 1))
bigNumber = Array(repeating: 0, count: A.count)
smallNumber = Array(repeating: 0, count: A.count)
for i in stride(from: A.count - 1, through: 0, by: -1) {
bigNumber[i] = Int(A[A.index(A.startIndex, offsetBy: i)].asciiValue! - 48)
}
smallNumberIndex = B.count - 1
for i in stride(from: A.count - 1, through: A.count - B.count, by: -1) {
smallNumber[i] = Int(B[B.index(B.startIndex, offsetBy: smallNumberIndex)].asciiValue! - 48)
smallNumberIndex -= 1
}
smallNumberIndex = B.count - 1
for i in stride(from: A.count - 1, through: A.count - B.count, by: -1) {
for j in stride(from: A.count - 1, through: 0, by: -1) {
multiple[smallNumberIndex + j] += bigNumber[j] * smallNumber[i]
}
smallNumberIndex -= 1
}
}
for i in stride(from: multiple.count - 1, to: 0, by: -1) {
if multiple[i] >= 10 {
multiple[i - 1] += multiple[i] / 10
multiple[i] %= 10
}
}
for number in multiple {
answer += "\(number)"
}
//MARK: - Output
print(answer)
}
solution()
์ ์ฒด์ฝ๋๋ ์ฌ๊ธฐ์์ ํ์ธํ ์ ์์ต๋๋ค.
'Swift Data Structure And Algorithm > String Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฌธ์์ด ํฌํจ๊ด๊ณ ์กฐ์ฌ (0) | 2022.03.10 |
---|---|
๋ฌธ์์ด ์ ๋ ฌ (0) | 2022.03.10 |
ํฐ ์๋ฆฟ์ ๋บ์ (0) | 2022.03.10 |
ํฐ ์๋ฆฟ์ ๋ง์ (0) | 2022.03.10 |
๋ฌธ์์ด ์์ถ (0) | 2022.03.10 |