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

[Swift Algorithm] ๊ฐœ์ˆ˜ ์„ธ๊ธฐ BOJ #10807

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

์ด N๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ •์ˆ˜ v๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ •์ˆ˜๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด์ ธ์žˆ๋‹ค. ์…‹์งธ ์ค„์—๋Š” ์ฐพ์œผ๋ ค๊ณ  ํ•˜๋Š” ์ •์ˆ˜ v๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜์™€ v๋Š” -100๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉฐ, 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ N๊ฐœ์˜ ์ •์ˆ˜ ์ค‘์— v๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

 

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

์ž…๋ ฅ1

11
1 4 1 2 4 2 4 2 3 4 4
2

์ถœ๋ ฅ1

3

 

์ž…๋ ฅ2

11
1 4 1 2 4 2 4 2 3 4 4
5

์ถœ๋ ฅ2

0

 

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

let count = readLine()!
let nums = readLine()!.components(separatedBy: " ").map({String($0)})
let v = readLine()!
var result = 0

for i in nums {
    if String(i) == v { result += 1 }
}

print(result)
  • ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ์ˆซ์ž๋“ค์„ ์ž…๋ ฅ๋ฐ›๊ณ , String ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.
  • ์ž…๋ ฅ๋œ ์ˆซ์ž๋“ค์ธ nums ๋ฐฐ์—ด์„ ์ˆœ์„œ๋Œ€๋กœ ๊ฒ€์‚ฌํ•˜๋ฉฐ v์™€ ๊ฐ™์„ ์‹œ result์— +1 ํ•ด์ฃผ์—ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฐฉ๋ฒ•๋งŒ ์•Œ๊ณ  ์žˆ๋‹ค๋ฉด ์‰ฌ์šด ๋ฌธ์ œ์˜€๋‹ค.

 


 

๋ฌธ์ œ

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

 

๋Œ“๊ธ€