포스트

3. 문자열 반복해서 출력하기

나의 코드

1
2
3
4
5
6
7
8
9
10
11
12
import Foundation

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

var answer : String = ""

for i in 0 ... a-1 {
    answer += s1
}

print(answer)

이렇게 생각한 이유. 반복문을 이용하여 풀어야한다고 생각을 했으며, 단순히 print()를 하게되면 아래와 같이 나온다는것을 생각하였다.

1
2
3
4
5
string
string
string
string
string

그래서 새로운 변수 answer를 하나 만들어 string을 누적으로 붙이게 하였다.

다른 풀이를 보니 여러 방법으로 한 것을 알았다. 일단 아래에 복사를 해두고 나중에 개념을 찾아보면서 내것으로 만들어야겠다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import Foundation

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

print(String(repeating: s1, count: a))

//

let inputValue = readLine()!.split(separator: " ").map {String($0) }
print(String(repeating: inputValue[0], count: Int(inputValue[1])! ))

//

import Foundation

let inp = readLine()!.components(separatedBy: [" "]).map { $0 }
let (s1, a) = (inp[0], Int(inp[1])!)
for i in 0 ..< a {
    print(s1, terminator: "")
}

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.