๋ฐฑ์ค€ - ์ฃผ์‚ฌ์œ„๊ฒŒ์ž„ 2476๋ฒˆ (Swift)

2022. 10. 3. 14:13ใ†Algorithm

๋ฐฑ์ค€ - ์ฃผ์‚ฌ์œ„๊ฒŒ์ž„ 2476๋ฒˆ (Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

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

 

2476๋ฒˆ: ์ฃผ์‚ฌ์œ„ ๊ฒŒ์ž„

์ฒซ์งธ ์ค„์—๋Š” ์ฐธ์—ฌํ•˜๋Š” ์‚ฌ๋žŒ ์ˆ˜ N์ด ์ฃผ์–ด์ง€๊ณ  ๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์‚ฌ๋žŒ๋“ค์ด ์ฃผ์‚ฌ์œ„๋ฅผ ๋˜์ง„ 3๊ฐœ์˜ ๋ˆˆ์ด ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ๊ฐ๊ฐ ์ฃผ์–ด์ง„๋‹ค. 

www.acmicpc.net

 

 

 ๋‚˜์˜ ํ’€์ด

 

๋ช‡ ๊ฐ€์ง€ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ์‹คํŒจํ•˜๋Š” ํ’€์ด์ด๋‹ค. ์•„๋งˆ๋„ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์˜ ์ตœ๋Œ€๊ฐ’์—์„œ filter์™€ map์„ ์—ฐ์†์œผ๋กœ ๋Œ๋ ค์„œ ๊ทธ๋Ÿฐ ๊ฒƒ ๊ฐ™๋‹ค.

import Foundation

func calculate(_ a: Int, _ b: Int, _ c: Int) -> Int {
  var ch = [Int](repeating: 0, count: 7)
  ch[a] += 1
  ch[b] += 1
  ch[c] += 1
  if ch.max()! == 3 {
    let val = ch.enumerated().filter{ $0.element == 3 }.map{ $0.offset }[0]
    return 10000+(val*1000)
  } else if ch.max()! == 2 {
    let val = ch.enumerated().filter{ $0.element == 2 }.map{ $0.offset }[0]
    return 1000+(val*100)
  } else {
    return ch.enumerated().sorted(by: >)[0].offset * 100
  }
}

let n = Int(readLine()!)!
var maxPrice = 0
for _ in 0..<n {
  let input = readLine()!
  let result = input.components(separatedBy: " ").map{Int($0)!}
  let price = calculate(result[0], result[1], result[2])
  print(price)
  maxPrice = max(maxPrice, price)
}

print(maxPrice)

 

๋‹ค๋ฅธ ํ’€์ด

 

๋ฌธ์ œ๋Š” 3๊ฐœ์˜ ์ฃผ์‚ฌ์œ„๋งŒ ๋Œ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— 3๊ฐœ๋ฅผ if๋ฌธ์œผ๋กœ ์ชผ๊ฐœ์„œ ํ’€์—ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ’€๋ฉด n๋ฒˆ๋งŒํผ๋งŒ ๋กœ์ง์„ ๋Œ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ํ˜„์ €ํžˆ ์ค„์–ด๋“ ๋‹ค.

import Foundation

let n = Int(readLine()!)!
var maxPrice = 0
for _ in 0..<n {
  let input = readLine()!
  let result = input.components(separatedBy: " ").map{Int($0)!}
  let a = result[0], b = result[1], c = result[2]
  var price = 0
  if a==b && b==c {
      price = 10000+(a*1000)
  } else if a==b || a==c {
      price = 1000+(a*100)
  } else if b==c {
      price = 1000+(b*100)
  } else {
      var maxVal = 0
      maxVal = max(a, b)
      maxVal = max(maxVal, c)
      price = maxVal * 100
  }
  maxPrice = max(maxPrice, price)
}

print(maxPrice)

 

 

 ํ”ผ๋“œ๋ฐฑ

 

๋ฌธ์ œ์˜ ํฌ๊ธฐ๊ฐ€ ์ž‘๋‹ค๋ฉด ํ•˜๋“œ์ฝ”๋”ฉ์œผ๋กœ ํ•ด๊ฒฐํ•˜๋Š” ๊ฒŒ ๋•Œ๋กœ๋Š” ์ข‹๋‹ค