Swift Data Structure And Algorithm/String Algorithm

ํฐ ์ž๋ฆฟ์ˆ˜ ๊ณฑ์…ˆ

youngjaeLee1026 2022. 3. 10. 19:14

1. ๋ฌธ์ œ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-03-10 19 10 48

2. ์ž…์ถœ๋ ฅ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-03-10 19 11 00

3. ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-03-10 19 11 12

4. ๋ฌธ์ œ ์„ค๊ณ„

  1. Swift์˜ ๊ฒฝ์šฐ ๊ธฐ๋ณธ Integer ์ž๋ฃŒํ˜•์˜ ์ตœ๋Œ€ ํฌ๊ธฐ๋Š” 21์–ต์ด๋ฏ€๋กœ 10^100 ์ˆซ์ž๋ฅผ ๋‹ด์„ ์ˆ˜ ์—†์Œ
  2. ๋”ฐ๋ผ์„œ ๋ฐฐ์—ด์— ๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ๋‹ด๊ณ , ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•˜๊ณ  ๋ฌธ์ž์—ด์— ์ €์žฅํ•จ์œผ๋กœ์จ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Œ
  3. ๋จผ์ € ๋” ํฐ ์ˆซ์ž๊ฐ€ ๋ฌด์—‡์ธ์ง€ ๊ธธ์ด๋ฅผ ํ†ตํ•ด ๋” ํฐ ์ˆซ์ž๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ์Œ.
  4. ๊ทธ ํ›„, ๊ฐ ์ˆซ์ž์— ํ•ด๋‹นํ•˜๋Š” ๋ฐฐ์—ด์— ์ˆซ์ž๋ฅผ ๊ฐ ์ž๋ฆฟ์ˆ˜ ๋งˆ๋‹ค ๋‹ด์Œ
  5. ์—ฌ๊ธฐ์„œ ๊ณฑ์…ˆ ๊ฒฐ๊ณผ๋Š” ๋” ํฐ ์ˆซ์ž์˜ ๊ธธ์ด + (์ž‘์€ ์ˆซ์ž์˜ ๊ธธ์ด - 1) ์ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๊ณ , ์ž‘์€ ์ˆซ์ž์˜ ๊ฐ ์ž๋ฆฌ์—์„œ ํฐ ์ˆซ์ž๋ฅผ ๊ณฑํ•œ ๊ฐ’์„
  6. ํ•œ ์นธ์”ฉ ์•ž์œผ๋กœ ๋ฐ€๋ ค๋‚˜๊ฐ€๋ฉฐ ๋”ํ•ด์ง€๋Š” ํŒจํ„ด์„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ์Œ
  7. ๊ฐ€๋ น, 123 * 12์˜ ๊ฒฝ์šฐ 123 * 2 = 246 + 123 * 1 = 1230 ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ๋ณด๋ฉด ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์›€
  8. ๊ณฑ์…ˆ ์—ฐ์‚ฐ์„ ๋ˆ„์ ํ•ด์„œ ๋”ํ•ด์„œ ๊ณฑ์…ˆ ๋ฐฐ์—ด์— ์ €์žฅ ํ›„, 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()

 

์ „์ฒด์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.