๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
3๏ธโƒฃ Swift/Problem Solving

[Swift Algorithm] ์ˆซ์ž์˜ ๊ฐœ์ˆ˜ BOJ #2577

by seolhee2750 2021. 7. 28.
๋ฌธ์ œ ์„ค๋ช…

์„ธ ๊ฐœ์˜ ์ž์—ฐ์ˆ˜ A, B, C๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ A × B × C๋ฅผ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ์— 0๋ถ€ํ„ฐ 9๊นŒ์ง€ ๊ฐ๊ฐ์˜ ์ˆซ์ž๊ฐ€ ๋ช‡ ๋ฒˆ์”ฉ ์“ฐ์˜€๋Š”์ง€๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์˜ˆ๋ฅผ ๋“ค์–ด A = 150, B = 266, C = 427 ์ด๋ผ๋ฉด A × B × C = 150 × 266 × 427 = 17037300 ์ด ๋˜๊ณ , ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ 17037300 ์—๋Š” 0์ด 3๋ฒˆ, 1์ด 1๋ฒˆ, 3์ด 2๋ฒˆ, 7์ด 2๋ฒˆ ์“ฐ์˜€๋‹ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— A, ๋‘˜์งธ ์ค„์— B, ์…‹์งธ ์ค„์— C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. A, B, C๋Š” ๋ชจ๋‘ 100๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 1,000๋ณด๋‹ค ์ž‘์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” A × B × C์˜ ๊ฒฐ๊ณผ์— 0 ์ด ๋ช‡ ๋ฒˆ ์“ฐ์˜€๋Š”์ง€ ์ถœ๋ ฅํ•œ๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ์—ด ๋ฒˆ์งธ ์ค„๊นŒ์ง€ A × B × C์˜ ๊ฒฐ๊ณผ์— 1๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž๊ฐ€ ๊ฐ๊ฐ ๋ช‡ ๋ฒˆ ์“ฐ์˜€๋Š”์ง€ ์ฐจ๋ก€๋กœ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ์ œ

์ž…๋ ฅ

150
266
427

์ถœ๋ ฅ

3
1
0
2
0
0
0
2
0
0

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import Foundation

let A = Int(readLine()!)!
let B = Int(readLine()!)!
let C = Int(readLine()!)!
let multi = String(A*B*C)
var result = Array(repeating: 0, count: 10)

for i in multi {
    result[Int(String(i))!] += 1
}

print(result.map({String($0)}).joined(separator : "\n"))
  • ์„ธ ๊ฐ€์ง€ ์ž…๋ ฅ์„ ๋ชจ๋‘ ๊ณฑํ•œ ๊ฐ’์„ String์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ์—ˆ๋‹ค.
  • 0๋ถ€ํ„ฐ 9๊นŒ์ง€ ์ˆซ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜๊ธฐ ์œ„ํ•ด 10์นธ์งœ๋ฆฌ result ๋ฐฐ์—ด์„ ์ƒ์„ฑํ–ˆ๋‹ค.
  • multi ๋ฌธ์ž์—ด์„ ์ˆœ์„œ๋Œ€๋กœ ๊ฒ€์‚ฌํ•˜๋ฉฐ result ๋ฐฐ์—ด์˜ ํ•ด๋‹นํ•˜๋Š” ์œ„์น˜์— 1์”ฉ ๋”ํ•ด์ฃผ์—ˆ๋‹ค.
  • ๊ฐœํ–‰์œผ๋กœ ์ถœ๋ ฅ์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด joined()ํ•จ์ˆ˜์— "\n" ๊ตฌ๋ถ„์ž๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • joined ํ•จ์ˆ˜ separator๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋งŒ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ์‰ฌ์šด ๋ฌธ์ œ์˜€๋‹ค.

 


 

๋ฌธ์ œ

https://www.acmicpc.net/problem/2577

๋Œ“๊ธ€