[Python To Swift] 백준 단계별로 풀어보기-입출력과 사칙연산

Cobugi·2021년 8월 10일
0

백준

목록 보기
1/21
post-thumbnail

python으로만 풀던 문제들을 처음부터 하나씩 swift로 바꿔보자


1000_A+B

  • Python
a, b = map(int, input().split())
print(a+b)
  • Swift
import Foundation

let line = readLine()!
let lineArr = line.components(separatedBy: " ")
let a = Int(lineArr[0])!
let b = Int(lineArr[1])!
print(a+b)

1001_A-B

  • Python
a, b = map(int, input().split())
print(a-b)
  • Swift
import Foundation

let line = readLine()!
let lineArr = line.components(separatedBy: " ")
let a = Int(lineArr[0])!
let b = Int(lineArr[1])!
print(a-b)

10998_A*B

  • Python
a, b = map(int, input().split())
print(a*b)
  • Swift
import Foundation

let line = readLine()!
let lineArr = line.components(separatedBy: " ")
let a = Int(lineArr[0])!
let b = Int(lineArr[1])!
print(a*b)

1008_A/B

  • Python
a, b = map(float, input().split())
print(a/b)
  • Swift
import Foundation

let inputLine = readLine()!
let inputLineArray = inputLine.components(separatedBy: " ")
let a = Double(inputLineArray[0])!
let b = Double(inputLineArray[1])!
print(a / b)

10869_사칙연산

  • Python
a, b = map(int, input().split())
print(a+b)
print(a-b)
print(a*b)
print(a//b)
print(a%b)
  • Swift
import Foundation

let inputLine = readLine()!
let inputLineArray = inputLine.components(separatedBy: " ")
let a = Int(inputLineArray[0])!
let b = Int(inputLineArray[1])!

print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(a%b)

10430_나머지

  • Python
A, B, C = map(int, input().split())
print((A+B)%C)
print(((A%C) + (B%C))%C)
print((A*B)%C)
print(((A%C) * (B%C))%C)
  • Swift
import Foundation

let inputLine = readLine()!
let inputLineArray = inputLine.components(separatedBy: " ")
let A = Int(inputLineArray[0])!
let B = Int(inputLineArray[1])!
let C = Int(inputLineArray[2])!

print((A + B) % C)
print(((A % C) + (B % C)) % C)
print((A * B) % C)
print(((A % C) * (B % C)) % C)

2588_곱셈

  • Python
a = int(input())
b = input()

b_1 = int(b[2])
b_10 = int(b[1])
b_100 = int(b[0])

print(a*b_1)
print(a*b_10)
print(a*b_100)
print(a*int(b))
  • Swift
import Foundation

let inputFirstLine = Int(readLine()!)!
let inputSecondLine = readLine()!

let inputSecondLineArray = Array(inputSecondLine)
//let inputSecondLineArray = inputSecondLine.map {String($0)}

let units = Int(String(inputSecondLineArray[2]))!
let tens = Int(String(inputSecondLineArray[1]))!
let hundreds = Int(String(inputSecondLineArray[0]))!

print(inputFirstLine * units)
print(inputFirstLine * tens)
print(inputFirstLine * hundreds)
print(inputFirstLine * Int(inputSecondLine)!)
profile
iOS Developer 🐢

0개의 댓글