[Python To Swift] 백준 단계별로 풀어보기-while문

Cobugi·2021년 8월 24일
0

백준

목록 보기
7/21
post-thumbnail

Python to Swift

while 문


10952_A+B - 5

  • Python
while True:
    a, b = map(int, input().split())
    if a == 0 and b == 0:
        break
    else:
        print(a+b)
  • Swift
import Foundation

while true {
    let inputLine = readLine()!.components(separatedBy: " ")
    let a = Int(inputLine[0])!
    let b = Int(inputLine[1])!
    if a == 0 && b == 0 {
        break
    } else {
        print(a + b)
    }
}

10951_A+B - 4

  • Python
while True:
  try:
    a, b = map(int, input().split())
    print(a + b)
  except:
    break
  • Swift
import Foundation

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

1110_더하기 사이클

  • Python
n = int(input())

def cycle(n):
    if n >= 10:
        first_num = n // 10
        second_num = n % 10
    else:
        first_num = 0
        second_num = n
        
    sum_num_second = (first_num + second_num) % 10
    new_num = second_num * 10 + sum_num_second
    return new_num

count = 1
next_level = cycle(n)
while n != next_level:
    count += 1
    next_level = cycle(next_level)
    
print(count)
  • Swift
let inputLine = Int(readLine()!)!

func cycle(_ num: Int) -> Int {
    let secondNumber = num % 10
    var sumNumber: Int
    if num < 10 {
        sumNumber = num
    } else {
        sumNumber = (Int(num / 10) + (num % 10)) % 10
    }
    return (secondNumber * 10) + sumNumber
}

var count = 1
var temp = cycle(inputLine)
while inputLine != temp {
    temp = cycle(temp)
    count += 1
}
print(count)
profile
iOS Developer 🐢

0개의 댓글