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

Cobugi·2021년 8월 13일
0

백준

목록 보기
2/21
post-thumbnail

Python to Swift

for 문


2739_구구단

  • Python
n = int(input())

for i in range(1, 10):
    print("{} * {} = {}".format(n, i, n*i))
  • Swift
import Foundation

if let tempN = readLine() {
    let n = Int(tempN)!
    for i in 1...9 {
        print("\(n) * \(i) = \(n * i)")
    }
}

10950_A+B-3

  • Python
a = int(input())
for _ in range(a):
    b, c = map(int, input().split())
    print(b+c)
  • Swift
import Foundation

if let tempT = readLine() {
    if let t = Int(tempT) {
        for _ in 1...t {
            if let inputLine = readLine() {
                let inputLineArray = inputLine.components(separatedBy: " ")
                if let a = Int(inputLineArray[0]), let b = Int(inputLineArray[1]) {
                    print(a + b)
                }
            }
        }
    }
}

8393_합

  • Python
n = int(input())
print(int(n*(n+1)/2))
  • Swift
import Foundation

let n = Int(readLine()!)!
print(n*(n+1)/2)

2741_N 찍기

  • Python
a = int(input())
for i in range(1, a+1):
    print(i)
  • Swift
let n = Int(readLine()!)!
for i in 1...n {
    print(i)
}

2741_기찍 N

  • Python
n = int(input())
for i in range(n, 0, -1):
    print(i)
  • Swift
let n = Int(readLine()!)!
for i in stride(from: n, through: 1, by: -1) {
    print(i)
}

11021_A+B-7

  • Python
a = int(input())
for i in range(1, a+1):
    b, c = map(int, input().split())
    print("Case #{}: {}".format(i, b+c))
  • Swift
import Foundation

if let tempT = readLine() {
    if let t = Int(tempT) {
        for i in 1...t {
            if let inputLine = readLine() {
                let inputLineArray = inputLine.components(separatedBy: " ")
                if let a = Int(inputLineArray[0]), let b = Int(inputLineArray[1]) {
                    print("Case #\(i): \(a + b)")
                }
            }
        }
    }
}

11022_A+B-8

  • Python
a = int(input())
for i in range(1, a+1):
    b, c = map(int, input().split())
    print("Case #{}: {} + {} = {}".format(i, b, c, b+c))
  • Swift
import Foundation

if let tempT = readLine() {
    if let t = Int(tempT) {
        for i in 1...t {
            if let inputLine = readLine() {
                let inputLineArray = inputLine.components(separatedBy: " ")
                if let a = Int(inputLineArray[0]), let b = Int(inputLineArray[1]) {
                    print("Case #\(i): \(a) + \(b) = \(a + b)")
                }
            }
        }
    }
}

2438_별 찍기 - 1

  • Python
n = int(input())
for i in range(1,n+1):
    print('*'*i)
  • Swift
let n = Int(readLine()!)!

for i in 1...n {
    for _ in 1 ... i {
        print("*", terminator: "")
    }
    print()
}

2439_별 찍기 - 2

  • Python
n = int(input())
for i in range(1,n+1):
    print(' ' * (n-i) + '*' * i)
  • Swift
let n = Int(readLine()!)!

for i in 1...n {
    if i < n {
        for _ in 1...n-i {
            print(" ", terminator: "")
        }
    }
    for _ in 1...i {
        print("*", terminator: "")
    }
    print()
}

10871_X 보다 작은 수

  • Python
n, x = map(int, input().split())

li_a = list(map(int, input().split()))

for a in li_a:
    if a < x:
        print(a, end=' ')
  • Swift
import Foundation

let inputLine = readLine()!.components(separatedBy: " ")
let n = Int(inputLine[0])!
let x = Int(inputLine[1])!

let inputLineOfN = readLine()!
    .components(separatedBy: " ")
    .map { Int($0)! }
    .filter { $0 < x }
for i in inputLineOfN {
    print(i, terminator: " ")
}
profile
iOS Developer 🐢

0개의 댓글