youngjaeLee1026 2022. 3. 10. 19:04

1. 문제

스크린샷 2022-03-10 19 01 52

2. 입출력

스크린샷 2022-03-10 19 02 15

3. 입출력 예시

스크린샷 2022-03-10 19 02 30

4. 문제 설계

  1. Swift의 경우 기본 Integer 자료형의 최대 크기는 21억이므로 10^100 숫자를 담을 수 없음
  2. 따라서 배열에 각 자릿수를 담고, 연산을 진행하고 문자열에 저장함으로써 문제를 해결할 수 있음
  3. 먼저 더 큰 숫자가 무엇인지 길이를 통해 파악하고, 각 숫자에 해당하는 배열에 숫자를 각 자릿수 마다 담음
  4. 덧셈 연산을 합 배열에 저장 후, 10이 넘어가면 올림수 처리를 함으로써 문제를 해결함

5. 전체 코드

//
//  main.swift
//  BigNumberSummation
//
//  Created by 이영재 on 2022/03/10.
//MARK: - 큰 자릿수 덧셈

//MARK: - Framework
import Foundation

//MARK: - Function
func solution() -> Void {
    //MARK: - Input
    guard let numberOne: String = readLine() else { return }
    guard let numberTwo: String = readLine() else { return }
    var sum: [Int] = []
    var bigNumber: [Int] = []
    var smallNumber: [Int] = []
    var smallNumberIndex: Int = 0
    var flag: Bool = false
    var answer: String = ""
    
    //MARK: - Process
    if numberOne.count < numberTwo.count {
        flag = true
    }
    
    if (flag) {
        sum = Array(repeating: 0, count: numberTwo.count)
        bigNumber = Array(repeating: 0, count: numberTwo.count)
        smallNumber = Array(repeating: 0, count: numberTwo.count)

        for i in stride(from: numberTwo.count - 1, through: 0, by: -1) {
            bigNumber[i] = Int(numberTwo[numberTwo.index(numberTwo.startIndex, offsetBy: i)].asciiValue! - 48)
        }
        
        smallNumberIndex = numberOne.count - 1
        for i in stride(from: numberTwo.count - 1, through: numberTwo.count - numberOne.count, by: -1) {
            smallNumber[i] = Int(numberOne[numberOne.index(numberOne.startIndex, offsetBy: smallNumberIndex)].asciiValue! - 48)
            
            smallNumberIndex -= 1
        }
        
        for i in 0..<numberTwo.count {
            sum[i] = bigNumber[i] + smallNumber[i]
        }
    } else {
        sum = Array(repeating: 0, count: numberOne.count)
        bigNumber = Array(repeating: 0, count: numberOne.count)
        smallNumber = Array(repeating: 0, count: numberOne.count)
        
        for i in stride(from: numberOne.count - 1, through: 0, by: -1) {
            bigNumber[i] = Int(numberOne[numberOne.index(numberOne.startIndex, offsetBy: i)].asciiValue! - 48)
        }
        
        smallNumberIndex = numberTwo.count - 1
        for i in stride(from: numberOne.count - 1, through: numberOne.count - numberTwo.count, by: -1) {
            smallNumber[i] = Int(numberTwo[numberTwo.index(numberTwo.startIndex, offsetBy: smallNumberIndex)].asciiValue! - 48)
            
            smallNumberIndex -= 1
        }
        
        for i in 0..<numberOne.count {
            sum[i] = bigNumber[i] + smallNumber[i]
        }
    }
    
    for i in stride(from: sum.count - 1, to: 0, by: -1) {
        if sum[i] >= 10 {
            sum[i - 1] += 1
            sum[i] %= 10
        }
    }
    
    for number in sum {
        answer += "\(number)"
    }
    
    //MARK: - Output
    print(answer)
}
solution()

 

전체코드는 여기에서 확인할 수 있습니다.