본문 바로가기

 𝗔𝗣𝗣𝗟𝗘/ALGORITHM

[내일배움캠프] 데일리 루틴(iOS_3회차) - 직사각형 별찍기

이미지를 클릭하면 코딩테스트 페이지로 이동합니다

 

내 풀이

import Foundation

let n = readLine()!.components(separatedBy: [" "]).map { Int($0)! }
let (a, b) = (n[0], n[1])

for _ in 0..<b {
    print(Array(repeating: "*", count: a).joined())
}

 

let n = readLine()!.components(separatedBy: [" "]).map { Int($0)! }
let (a, b) = (n[0], n[1])

 

- readLine()!: 한 줄 입력

- components(separatedBy: [" "]): 공백을 기준으로 문자열 분리

- map { Int($0)! }: 각각의 문자열을 정수로 변환

- (a, b) = (n[0], n[1]): 각각 a와 b에 할당

 

for _ in 0..<b {
    print(Array(repeating: "*", count: a).joined())
}

 

- 0..<b: 범위에 대해 반복(세로의 길이 m에 해당)

- Array(repeating: "*", count: a): 각 반복마다 가로의 길이가 n이고 별(*)로 이루어진 배열 생성

- joined(): 사용하여 배열의 요소들을 문자열로 합치기

- 합쳐진 문자열 출력

Recent Posts
Visits
Today
Yesterday
Archives
Calendar
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31